我在使用this指针时在类构造函数中遇到了一个问题,但此时this指针恰好为NULL。例如MyClass::MyClass(){//thispointerhappentobeNULLinthiscase,anditcrashintehm_callbackfuncbecauseitdoesnothandlenullinput.m_callbackFunc(this);}我想知道为什么这个指针可以为空?在什么情况下这个指针可以为空? 最佳答案 this指针可以为NULL的唯一方法是程序中的某些代码表现出未定义的行为。例如可以通过类似的方
我正在从MFCCDialogEx派生一个类:classMyDialog:publicCDialogEx{public:virtualvoidPostNcDestroy();……};我实现了PostNcDestroyassuch:voidMyDialog::PostNcDestroy(){CDialogEx::PostNcDestroy();delete*this;//oops,typo}我很惊讶地看到这段代码编译(使用VC120或VisualStudio2013),并且根本没有生成任何警告。谁能告诉我为什么会这样?谢谢。 最佳答案
我有两个与C++相关的问题:在许多教科书中,关键字this是指向调用对象的指针。正确的?因为我喜欢玩编码,所以我编写了以下简单代码:structBase{voidg();virtualvoidf();};voidBase::f(){coutf();}structDerived:publicBase{intd;voidf();};voidDerived::f(){cout以上代码产生以下输出:sizeof(a):4sizeof(b):8Base::g()sizeof(*this):4Base::f()Base::g()sizeof(*this):4//why4bytesnot8bytes
我从here中读到有关调整器的信息.这是一些引述:Now,thereisonlyoneQueryInterfacemethod,buttherearetwoentries,oneforeachvtable.Rememberthateachfunctioninavtablereceivesthecorrespondinginterfacepointerasits"this"parameter.That'sjustfineforQueryInterface(1);itsinterfacepointeristhesameastheobject'sinterfacepointer.Buttha
以下代码是使用VC++2012年11月CTP编译的。但是编译器给出了警告。我只是想知道这是否是VC++2012年11月CTP的错误。structA{intn;A(intn):n(n){}intGet()const{returnn;}intGet(){////Ifusing"static_cast(*this).Get();"instead,thenOK.//returnstatic_cast(*this).Get();//Warning!}};intmain(){Aa(8);////warningC4717:'A::Get':recursiveonallcontrolpaths,//f
详细日志error:Cannotfigureouthowtosavethisfieldintodatabase.Youcanconsideraddingatypeconverterforit.privatefinaljava.util.DatemTime=null;我有一个字段为的实体varmStartTime:Date=Date()//java.util.Date为什么Room不能保留Date对象?什么是日期的最佳转换器? 最佳答案 Date正是https://developer.android.com/training/data
详细日志error:Cannotfigureouthowtosavethisfieldintodatabase.Youcanconsideraddingatypeconverterforit.privatefinaljava.util.DatemTime=null;我有一个字段为的实体varmStartTime:Date=Date()//java.util.Date为什么Room不能保留Date对象?什么是日期的最佳转换器? 最佳答案 Date正是https://developer.android.com/training/data
我得到了一个类,其中包含私有(private)的int变量x和y,以及一个运算符重载函数,classBag{private:intx;inty;public:Bag();~Bag();//.......//.....etc};Bagoperator+(Bagnew)const{Bagresult(*this);//whatdoesthismean?result.x+=new.x;result.y+=new.y;}“Bagresult(*this);”有什么作用?那里?。 最佳答案 Bagresult(*this)创建调用运算符函数的
我有一些宏需要访问当前类的类型,目前我通过违反DRY的模式解决了这个问题:structThisScruct{inta;doubleb;//examplestaticmethodusingthis-purelyexample-notfullusecasestaticsize_tsum_offsets(){typedefThisStructSelfT;returnoffsetof(SelfT,a)+offsetof(SelfT,b);}};这在使用offsetof时经常出现。关键字,至少在我自己的作品中是这样。现在,在您锁定this无法通过静态方法访问之前-意识到我只想知道如何以通用/宏友
背景:我有一个包含许多变量的复杂类。我有一个可靠且经过测试的复制构造函数:Applepie::Applepie(constApplepie©):m_crust(copy.m_crust),m_filling(copy.m_filling){}在初始化列表中调用的一些成员变量拷贝构造函数执行分配。问题:我需要创建operator=。我可以简单地执行以下操作,而不是使用赋值而不是初始化列表来复制现有的构造函数,并释放正在被替换的内存等等,而是可以简单地执行以下操作:Applepie&Applepie::operator=(constApplepie©){if(this!=&