草庐IT

address-operator

全部标签

c++ - 如何在 operator += 中调用 operator +

operator+=这样定义对吗?!voidoperator+=(constBigNumber&other){*this=(*this)+other;}在这样的类中:classBigNumber{public://....BigNumberoperator+(constBigNumber&other){returnsum(other);}//....} 最佳答案 是的。但正确的方法是根据operator+=来实现operator+:structfoo{intvalue;foo&operator+=(constfoo&other){v

c++ - ‘operator ==’ 中的 ‘a == b’ 不匹配

#includeusingnamespacestd;classfamily{private:doubleweight;doubleheight;public:family(doublex,doubley);~family();doublegetWeight();doublegetHeight();doublesetWeight();doublesetHeight();booloperator==(constfamily&,constfamily&);};boolfamily::operator==(constfamily&a,constfamily&b){return(a.getWei

c++ - 避免使用 "child"延迟 `operator<<` 对象构造

假设我有一个存储std::vector的容器对象多态child。structChild{Child(Parent&mParent){/*...*/}virtual~Child(){}};classParent{private:std::vector>children;templateauto&mkChild(TArgs&&...mArgs){//`static_assert`that`T`isderivedfrom`Child`...children.emplace_back(std::make_unique(std::forward(mArgs)...));return*childr

c++ - 多重继承 : Different Address same address

我写了一个示例程序。如果我打印pa和pb的地址都是不同的。你能告诉我为什么会这样吗?#includeusingnamespacestd;classA{intx;};classB{inty;};classC:publicA,publicB{intz;};intmain(){Cc;A*pa;B*pb;pa=&c;pb=&c;cout 最佳答案 作为KerrekSB把它,pa和pb在您的示例中,实际上并不指向c,而是指向A和Bc的子对象.通过多重继承,来自基类的数据本质上是一个接一个地堆叠起来。基类型指针只是偏移到该基类的数据。正因为如此

C++ 错误 : "taking address of temporary array"

我试图在if语句中声明一个数组。我以这种方式编写代码,以便在ifblock退出后对象保留在范围内,但现在我遇到了一个新问题:“获取临时数组的地址”。我怎样才能用另一种方式重写它,以便为maskArray分配正确的值?int*maskArray;if(conditional==true)maskArray=(int[9]){0,1,0,1,-4,1,0,1,0}; 最佳答案 假设您以后不打算修改maskArray指向的内容,那么最好/最简单的解决方案是:constint*maskArray;if(conditional==true){

c++ - operator-> 重复直到它返回一个非类类型的值

根据13.3.1.2/8,或更好footnote-129(强调我的):[...]Theprocessrepeatsuntilanoperator->functionreturnsavalueofnon-classtype.我以为我知道operator->是如何工作的(让我说,它是基于返回类型的递归方式),但我发现我完全不知道关于它实际上是如何工作的(我的意思是,它的返回类型)。当我找到它时,我想知道是否真的可以为通用结构S定义和使用类似doubleoperator->()的东西,因为我已经从来没有这样使用过这样的运算符。例如,请考虑以下代码:structS{constexprdoubl

c++ - 类型没有可行的重载 operator[]

我定义了一个类型:typedefunordered_map>Graph;我有一把key:stringv="A12";当我尝试使用key访问列表时:for(autow=g[v].begin();w!=g[v].end();w++){...}g是Graph类型,我得到错误:Noviableoverloadedoperator[]fortype'constGraph'我该如何解决这个问题? 最佳答案 问题是g是一个constgraph&。索引运算符[]可能需要为map创建一个新元素,从而改变它。这就是为什么你不能在constgraph&上

c++ - operator<< 无法输出 std::endl -- 修复?

下面的代码在应该只输出std::endl时出错了:#include#includestructMyStream{std::ostream*out_;MyStream(std::ostream*out):out_(out){}std::ostream&operatorstructFoo{OutputStream*out_;Foo(OutputStream*out):out_(out){}voidtest(){(*out_)foo(&out);foo.test();returnEXIT_SUCCESS;}错误是:stream1.cpp:19:error:nomatchfor'operato

c++ - STL operator= Visual Studio 2010 的行为发生变化?

我正在尝试使用VisualStudio2010(C++)编译QtScriptGenerator(gitorious),但遇到了编译错误。在寻找解决方案的过程中,我偶尔会看到自VS2008以来由于VS2010的STL实现的变化和/或c++0x一致性变化而引入的编译破损。知道下面发生了什么,或者我该如何解决它?如果有问题的代码似乎是QtScriptGenerator的,我想我会更容易修复它。但在我看来,有问题的代码可能在VS2010的STL实现中,我可能需要创建一个解决方法?附言。我对模板和STL很陌生。我有嵌入式和控制台项目的背景,这些项目直到最近才经常被避免以减少内存消耗和交叉编译器风

c++ - 重载 operator<< 以在不使用友元函数的情况下输出对象成员

隔了很长时间我正在刷新cpp,试图理解运算符重载方法。我试图重载“operator这是我的类定义:classAdd{private:intx;public:friendostream&operator函数实现//Method1ostream&operator主函数调用cout现在我的问题是,我想在不使用friend函数的情况下实现方法1的类型调用。但是不知道,在cpp中可不可以。我尝试了很少的实现,但都给我编译错误。请帮助我理解我在这里遗漏的要点。 最佳答案 如果你的类中有公共(public)访问函数,或者stream-就像一个,你