草庐IT

compound-operator

全部标签

C++ 使用 operator int() 而不是 operator+

我试图理解为什么调用operatorint()而不是定义的operator+classD{public:intx;D(){cout我的输出是:intCtorDintCtorDoperatorintoperatorint3DDtor2DDtor1 最佳答案 您的表达式D(1)+D(2)涉及临时对象。因此,您必须更改operator+的签名以通过const-ref#includeusingnamespacestd;classD{public:intx;D(){cout它打印:intCtorDintCtorDOP+intCtorDoper

c++ - "const T& operator[](size_type i)"中的 const 有什么用?

我在一本书http://www.acceleratedcpp.com/中发现了这个有趣的行-资源-第11章-Vec.h(我是一个std::vector翻版)而且我真的不明白这个版本的运算符有什么用。为什么要定义此运算符的两个版本(常量和非常量)?我什至试过了,在我看来,非常量版本一直被调用......你能解释一下吗?#include#include#include#includeusingnamespacestd;templateclassVec{public:typedefT*iterator;typedefconstT*const_iterator;typedefsize_tsiz

c++ - 为 Char 赋值重载 operator[] - C++

虽然我确实有一些编程经验,但我是C++的新手。我构建了一个Text类,它使用动态char*作为主要成员。类定义如下。#include#includeusingnamespacestd;classText{public:Text();Text(constchar*);//Typecastchar*toTextobjText(constText&);//Copyconstructor~Text();//OverloadedoperatorsText&operator=(constText&);Textoperator+(constText&)const;//Concatbooloperat

c++ - 是否可以使用 operator new 和 initialiser 语法初始化非 POD 数组?

我刚刚阅读并理解IsitpossibletoinitialiseanarrayinC++11byusingnewoperator,但这并不能完全解决我的问题。这段代码在Clang中给我一个编译错误:structA{A(intfirst,intsecond){}};voidmyFunc(){newA[1]{{1,2}};}我希望{{1,2}}用单个元素初始化数组,然后用构造函数参数{1,2}初始化,但我得到这个错误:error:nomatchingconstructorforinitializationof'A'newA[1]{{1,2}};^note:candidateconstruc

c++ - 错误 C2678 : binary '==' : no operator found which takes a left-hand operand of type (or there is no acceptable conversion)

我正在尝试编译以下代码:#include#include#includetypedefboost::geometry::model::d2::point_xyPoint;typedefstd::pairVector;booloperator==(constPoint&p1,constPoint&p2){returnp1.x()==p2.x()&&p1.y()==p2.y();}intmain(){Vectorvec1(Point(0,0),Point(1,1));Vectorvec2(Point(0,0),Point(1,2));std::coutVS2012C++编译器返回以下编译错

c++ - “no match for ' operator< '” 尝试插入到 std::set 时

我正在使用gcc4.3.3尝试编译以下代码:structtestStruct{intx;inty;booloperatorsetti;setti.insert(testStruct(10,10));return0;}我得到这个错误:/usr/include/c++/4.4/bits/STL_function.h|230|错误:‘__x我怀疑我没有像应该做的那样重载运算符,但我无法查明确切的问题。我在这里做错了什么? 最佳答案 运算符必须是const并且取一个const引用:booloperator

c++ - 不明确的 string::operator= 调用隐式转换为 int 和 string 的类型

给定以下程序:#include#includeusingnamespacestd;structGenericType{operatorstring(){return"HelloWorld";}operatorint(){return111;}operatordouble(){return123.4;}};intmain(){inti=GenericType();strings=GenericType();doubled=GenericType();cout它在VisualStudio11上编译,但不是clang或gcc。它有问题,因为它想从GenericType隐式转换为int为cha

c++ - 如何为聚合结构实现 C++ (in)equality operators?

有时我有这样的结构——structaggregate1{std::stringname;std::vectoroptions;size_tfoobar;//...};--其中(不)平等被简单地定义为所有成员的(不)平等:lhs_name==rhs_name&&lhs_options==rhs_options&&lhs_foobar==rhs_foobar.实现它的“最佳”方法是什么?(最好的是:(运行时-)效率、可维护性、可读性)operator==在operator!=方面operator!=在operator==方面==的单独实现和!=作为成员(member)还是作为免费功能?请注

c++ - '&' : illegal operation on bound member function expression

这个问题在这里已经有了答案:Printaddressofvirtualmemberfunction(5个答案)关闭7年前。当我尝试从具有主要功能的单个cpp文件时,这有效,sprintf(smem_options,"#transcode{vcodec=RV24}:smem{""video-prerender-callback=%lld,""no-time-sync},",(longlongint)(intptr_t)(void*)&cbVideoPrerender);如何在类中将函数参数传递给sprintf?sprintf(smem_options,"#transcode{vcodec

c++ Overload operator bool() 使用 operator+ 给出模糊的重载错误

我正在编译MegaInt类的一些C++代码,它是一个允许对大数进行算术运算的正十进制类型类。我想重载operatorbool以允许这样的代码:MegaIntm(45646578676547676);if(m)cout这是我做的:标题:classMegaInt{public:...operatorbool()const;};constMegaIntoperator+(constMegaInt&left,constMegaInt&right);constMegaIntoperator*(constMegaInt&left,constMegaInt&right);实现:MegaInt::op