草庐IT

send_this_email

全部标签

c++ - shared_ptr 和 this 指针

好的,我开始使用共享指针并尽可能多地传递共享指针。不再转换为原始指针。这很好用,除了在这种特定情况下:假设我们有一个类也是另一个类的观察者,如下所示:classMyClass:publicIObserver{public:MyClass(std::shared_ptrotherClass);voidDoSomethingImportant();private:std::shared_ptrm_otherClass;};这个类在我的应用中是这样使用的:std::shared_ptrmyInstance(newMyClass(otherInstance));...myInstance->D

C++ Lambda、捕获、智能 Ptr 和堆栈 : Why Does this Work?

我一直在研究C++11中的一些新特性,并尝试编写以下程序,但预计它不会运行。令我惊讶的是,它确实如此(在带有'std=c++0x'标志的Linuxx86上的GCC4.6.1上):#include#include#includestd::functioncount_up_in_2s(constintfrom){std::shared_ptrfrom_ref(newint(from));return[from_ref](){return*from_ref+=2;};}intmain(){autoiter_1=count_up_in_2s(5);autoiter_2=count_up_in_

c++ - 在这种情况下,C++ this 指针可以为 NULL

我在使用this指针时在类构造函数中遇到了一个问题,但此时this指针恰好为NULL。例如MyClass::MyClass(){//thispointerhappentobeNULLinthiscase,anditcrashintehm_callbackfuncbecauseitdoesnothandlenullinput.m_callbackFunc(this);}我想知道为什么这个指针可以为空?在什么情况下这个指针可以为空? 最佳答案 this指针可以为NULL的唯一方法是程序中的某些代码表现出未定义的行为。例如可以通过类似的方

c++ - 为什么 “delete *this” 会编译?

我正在从MFCCDialogEx派生一个类:classMyDialog:publicCDialogEx{public:virtualvoidPostNcDestroy();……};我实现了PostNcDestroyassuch:voidMyDialog::PostNcDestroy(){CDialogEx::PostNcDestroy();delete*this;//oops,typo}我很惊讶地看到这段代码编译(使用VC120或VisualStudio2013),并且根本没有生成任何警告。谁能告诉我为什么会这样?谢谢。 最佳答案

c++ - 关键字 "this"的实际含义是什么?

我有两个与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

c++ - 为什么我们需要 "this pointer adjustor thunk"?

我从here中读到有关调整器的信息.这是一些引述:Now,thereisonlyoneQueryInterfacemethod,buttherearetwoentries,oneforeachvtable.Rememberthateachfunctioninavtablereceivesthecorrespondinginterfacepointerasits"this"parameter.That'sjustfineforQueryInterface(1);itsinterfacepointeristhesameastheobject'sinterfacepointer.Buttha

c++ - 为什么 decltype(*this) 没有返回正确的类型?

以下代码是使用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

java - Android 房间 - 错误 : Cannot figure out how to save this field into database

详细日志error:Cannotfigureouthowtosavethisfieldintodatabase.Youcanconsideraddingatypeconverterforit.privatefinaljava.util.DatemTime=null;我有一个字段为的实体varmStartTime:Date=Date()//java.util.Date为什么Room不能保留Date对象?什么是日期的最佳转换器? 最佳答案 Date正是https://developer.android.com/training/data

java - Android 房间 - 错误 : Cannot figure out how to save this field into database

详细日志error:Cannotfigureouthowtosavethisfieldintodatabase.Youcanconsideraddingatypeconverterforit.privatefinaljava.util.DatemTime=null;我有一个字段为的实体varmStartTime:Date=Date()//java.util.Date为什么Room不能保留Date对象?什么是日期的最佳转换器? 最佳答案 Date正是https://developer.android.com/training/data

c++ - 关于c++中 "this"指针的问题

我得到了一个类,其中包含私有(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)创建调用运算符函数的