草庐IT

delete-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++ - 为 std::shared_ptr 指定一个删除器,它适用于特定类型或其派生类型的所有对象

我的项目中有以下类classBase{public:virtual~Base(){};}classDer1:publicBase{public:virtual~Der1(){};}classDer2:publicBase{public:virtual~Der2(){};}我将这些类的对象保存为std::shared_ptr。我需要为类型为Base或其任何derivedtypes的所有对象提供自定义删除器。我想要的删除方法中的代码将对所有这些对象做同样的事情,比如classDeleter{public:voidoperator()(Base*b){//Dosomethingdeleteb

c++ - 在什么情况下 delete 运算符会抛出错误?

在我的链表实现中,删除内部类Node实例的辅助函数deleteNode(Node*)抛出运行时错误,通过“触发断点”在VS2015的本地Windows调试器中。我小心地匹配我的new和delete运算符。范围/引用是否发挥了我没有意识到的作用?即使clear()中存在逻辑错误并且deleteNode()被传递给nullptr,它也不应该抛出错误删除nullptr,然后将nullptr赋值给自己吧?该删除有什么问题?classLinkedList{public:LinkedList():head(nullptr){}~LinkedList(){clear();}voidpush_fron

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++ 删除 static_cast<void*> (指针)行为

假设代码执行以下操作:T*pointer=newT();deletestatic_cast(pointer);结果是什么?未定义,内存泄漏,内存被删除? 最佳答案 行为未定义。关于delete表达式,C++标准说:Inthefirstalternative(deleteobject),ifthestatictypeoftheoperandisdifferentfromitsdynamictype,thestatictypeshallbeabaseclassoftheoperand’sdynamictypeandthestaticty

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

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