草庐IT

delete-operator

全部标签

c++ - 循环依赖 : can't delete an incomplete type

我不明白为什么会出现此编译器错误:errorC2027:useofundefinedtype'GameState'note:seedeclarationof'GameState'errorC2338:can'tdeleteanincompletetypewarningC4150:deletionofpointertoincompletetype'GameState';nodestructorcalled这是相关代码:#pragmaonce#include#include"SpawnManager.h"#include"Resource.h"#include#includeclassGa

c++ - 重载 operator= 中断 std::sort

可能是个骗子,但我找不到。在用双节棍敲打我的键盘两天后,我发现重载等号运算符(operator=)显然会破坏std::sort。也许我错误地重载了operator=?这是我的MCVE:#include#include#include#include#include#includestructPerson{std::stringname;uint32_tage;booloperatorage&people){std::coutpeople={{"james",12},{"jada",4},{"max",44},{"bart",7}};PrintPeople(people);std::so

c++ - 是否需要将 delete 用于在 vector 中创建的新数组?

我正在尝试为void**数据数组创建一些动态数组。std::vectordata;data.push_back(newdouble[1024]);//arrayofdoublesdata.push_back(newshort[1024]);//arrayofshorts为了清理我应该只使用data.clear();或者是否需要删除每个新的(s),如果需要,如何完成? 最佳答案 ForcleanupshouldIjustusedata.clear();这将从vector中删除所有指针。如果其中任何一个是指向它们各自动态对象的唯一拷贝,

c++ - 如果我有运算符 T *(),是否需要重载 delete?

如果我有一个包含指针的模板类A,并且A有一个将返回该指针的隐式转换运算符,我是否需要,或者我应该,为A定义一个delete运算符,如果我打算将delete应用于此类的对象? 最佳答案 如果定义operatornew,则只需定义operatordelete——在这种情况下,您几乎必须这样做。这并不意味着某些东西不需要删除您的A*——但您不需要为此定义任何运算符,它会默认工作。 关于c++-如果我有运算符T*(),是否需要重载delete?,我们在StackOverflow上找到一个类似的问

C++ 为两个 double 重载 operator%

是否可以为两个double重载operator%?constdoubleoperator%(constdouble&lhs,constdouble&rhs){returnfmod(lhs,rhs);}当然,这会产生错误,因为两个参数之一必须具有类类型。所以我考虑利用C++隐式构造函数调用的可能性来解决这个问题。我是通过以下方式做到的:classMyDouble{public:MyDouble(doubleval):val_(val){}~MyDouble(){}doubleval()const{returnval_;}private:doubleval_;};constdoubleop

c++ - 是否可以在 C++ 中声明 operator= private 并同时由编译器合成

我对运算符=很满意,它由编译器自动合成。但我希望它是私有(private)的,并且不想用类型的页面长定义来膨胀我的代码Foo&Foo::operator=(constFoo&foo){if(this==&foo)return*this;member1_=foo.member1_;member2_=foo.member2_;member3_=foo.member2_;...member1000_=foo.member1000_;return*this;}请问有什么办法吗? 最佳答案 在C++11中是:classFoo{Foo&oper

c++ - 如何在 C++ 中将 operator= 与匿名对象一起使用?

我有一个带有重载运算符的类:IPAddress&IPAddress::operator=(IPAddress&other){if(this!=&other){deletedata;this->init(other.getVersion());other.toArray(this->data);}return*this;}当我尝试编译时:IPAddressx;x=IPAddress(IPV4,"192.168.2.10");我收到以下错误:main.cc:Infunction‘intmain()’:main.cc:43:39:error:nomatchfor‘operator=’in‘x

c++ - 在成员函数中使用 operator()

我在我的一个类中重载了运算符(),我想在另一个成员函数中使用它。classA{public:voidoperator()();voidoperator()(doublex);};voidA::operator()(){//stuff};voidA::operator()(doublex){//stuffwithothermembersandxthis->operator();};行this->operator()不起作用。我只想使用我定义为类A的成员函数的运算符。我得到的错误是:Error1errorC3867:'A::operator()':functioncallmissingar

c++ - C++ 中逗号分隔值的 `operator<<`

以下语法在OpenCV中有效MatR=(Mat_(4,4)怎么可能?哪个运算符重载了?这个表达的意义是什么?现在的C++可以重载逗号运算符吗? 最佳答案 可以重载逗号运算符,但通常不推荐这样做(在许多情况下,重载的逗号会造成混淆)。上面的表达式为4*4矩阵定义了16个值。如果您想知道这是怎么可能的,我将展示一个更简单的示例。假设我们希望能够写出类似的东西MyVectorR=(MyVector()然后我们可以定义MyVector使得和,运算符将新值附加到vector:templateclassMyVector:publicstd::v

c++ - 我可以使用 operator== 作为指针吗?

我创建了一些重载了operator==的对象。classCorridor{public:Corridor(intiStart,intiEnd);~Corridor();//Overloadedoperatorstosimplifysearchincontainer.friendbooloperator==(constCorridor&lhs,constintrhs);friendbooloperator==(constintlhs,constCorridor&rhs);protected:intm_iIntersectionIDStart;intm_iIntersectionIDEnd