草庐IT

compound-operator

全部标签

c++ - 简单的 C++ : How to overload the multiplication operator so that float*myClass and myClass*float works

classMyClass;intmain(){floata=5;MyClassc1;MyClassc2=a*c1;MyClassc3=c1*a;}如何重载乘法运算符以便a*c1和c1*a都能工作? 最佳答案 像这样:MyClassoperator*(floatx,constMyClass&y){//...}MyClassoperator*(constMyClass&y,floatx){//...}第二个也可以是成员函数:classMyClass{//...MyClassoperator*(floatx);};前2个选项作为类范围之外

c++ - 重载 operator== 提示 'must take exactly one argument'

我试图重载operator==,但编译器抛出以下错误:‘boolRationalnumber::operator==(Rationalnumber,Rationalnumber)’musttakeexactlyoneargument我的一小段代码如下:boolRationalnumber::operator==(Rationalnumberl,Rationalnumberr){returnl.numerator()*r.denominator()==l.denominator()*r.numerator();}声明:booloperator==(Rationalnumberl,Rati

c++ - 使用 std::map 时,我应该为键类型重载 operator== 吗?

std::map不应该有重复的键,所以当我有一个自定义类型时它怎么知道我有一个重复的键,我需要做重载运算符==吗?或者它会被隐式创建?根据文档,我只需要运算符考虑这个例子:classMyType{public:MyType(intnewId){id=newint;*id=newId;};~MyType{deleteid;}private:int*id;};intmain(){std::mapmyMap;std::pair::iterator,bool>ret;ret=myMap.insert(std::pair(myType(2),100));if(!ret.second){//now

c++ - 为什么只有 random-access-iterator 在 C++ 中实现 operator+?

我想为STLlist迭代器获取farnext值,但它没有实现operator+,不过vector有它。为什么以及如何获得我想要的值(value)?我想如果我多次调用operator++就可以做到这一点,但这不是有点脏吗?我想做的是:listl;...omitted...list::iteratoritr=l.begin()+3;//but,listiteratordoesnothave//operator+什么是我想要的最佳解决方案? 最佳答案 您想使用std::advance:list::iteratoritr=l.begin()

C++,opencv : Is it safe to use the same Mat for both source and destination images in filtering operation?

过滤操作涉及卷积,位置(x,y)处的过滤值也将取决于像素(x-a,y-b)和a的强度,b>0.因此直接使用同一图像作为目标将导致意外行为,因为在计算过程中我使用了一些已经过滤的数据而不是原始数据。问题opencv是否在cv::GaussianBlur(.)、cv::blur等函数内部管理这个问题?为src和dst参数提供对相同Mat的引用是否安全?谢谢 最佳答案 是的,如果你这样做就不会有任何问题。我做过几次这样的事情。openCV会自动处理它。我测试了下面的代码,它运行良好:intmain(intargc,char*argv[])

c++ - 为什么添加两个字符串文字不使用 operator+?

编辑:我重新格式化了帖子以使其更清晰。为什么这样做:structA{};structB{B(A){}};voidoperator+(constB&,constB&){}intmain(){Aa1,a2;a1+a2;}这不是吗?structB{B(constchar*){}};voidoperator+(constB&,constB&){}//error:invalidoperandsoftypes'constchar[6]'and'constchar[6]'tobinary'operator+'|intmain(){"Hello"+"world";}本质上,在第一个示例中,a1和a2都

c++ - 为什么这个 operator= 调用不明确?

我正在制作一个带有转发构造函数的瘦派生类。(请耐心等待,我必须使用缺少继承构造函数的GCC4.7.2)。第一次尝试时,我忘记添加explicit关键字,结果出错。有人可以准确解释为什么会出现此特定错误吗?我无法弄清楚事件的顺序。#includetemplatestructshared_ptr:std::shared_ptr{template/*explicit*/shared_ptr(Args&&...args):std::shared_ptr(std::forward(args)...){}};structA{};structConvertsToPtr{shared_ptrptr=s

c++ - `std::istream::operator>>()` 可以接受像 stdio 的 %i 格式说明符这样的整数基数前缀吗?

当使用scanf()及其变体时,格式说明符%i将接受十六进制(前缀为“0x”)、八进制(前缀为“0”)或十进制(无前缀)的数据,例如字符串“0x10”、“020”和“16”都被转换为十进制值为16的整数。这可以用std::istream::operator>>格式的输入来完成吗?使用没有i/o操纵器的普通>>i将“0x10”转换为零(或者更确切地说,前导0是,不处理“x10”部分),并且“020”到20。hex、oct和dec操纵符的行为类似于%x、%o和%d分别。我正在寻找像%i一样工作的通用整数输入操纵器。有趣的是,hex操纵器可能同时接受“0x10”和“10”,将它们转换为16进

c++ - std::valarray 的 operator* 有什么问题?

考虑以下MCVE,其中我有两个值数组,其中w是两倍v(tryitouthere):#includeusingnamespacestd;intmain(){valarrayv{1,2,3};for([[maybe_unused]]autox:v){}//Okautow=v*2;//Leadstofailureinloopbelow//valarrayw=v*2;//Works//autow=v*=2;//Works//autow=v;w*=2;//Worksfor([[maybe_unused]]autox:w){}//Failurehere}此示例在最后一个循环中使用clang和gcc

c++ - 为什么 operator* of rvalue unique_ptr 返回一个左值?

使用来自“死的”unique_ptr的operator*的返回值是不好的。以下代码可以编译,但当然会导致未定义行为:auto&ref=*std::make_unique(7);std::cout为什么标准没有将std::unique_ptr右值的operator*的返回类型设为内部值的右值,而不是左值,像这样://couldhavebeendoneinsideunique_ptrT&operator*()&{return*ptr;}T&&operator*()&&{returnstd::move(*ptr);}在这种情况下这会工作正常:std::cout(7)但是开头的代码无法编译(无