草庐IT

this_record

全部标签

c++ - 函数调用的 "this"的评估是否以相对于参数的未指定顺序进行?

众所周知(虽然不够广泛>.puts()可以任意顺序出现,作为任意编译器选择:#includeintFunction1(){std::puts("Function1");return1;}intFunction2(){std::puts("Function2");return2;}intAdd(intx,inty){returnx+y;}intmain(){returnAdd(Function1(),Function2());}但是,这是否也适用于.左侧的this的求值,.*,->或->*运算符?换句话说,puts()下面的也是未指定的顺序吗?#includestructStruct{S

c++ - 为什么成员函数尝试 block 处理程序中的 lambda(捕获 'this')不能访问 VC++ 2013 中的私有(private)数据成员?

与thisquestionaboutstaticinitializers不同但可能相关.前两个函数编译良好,最后一个函数在vc++中不编译,但在clang和gcc中编译:classA{protected:std::stringprotected_member="yay";public:voidwithNormalBlock();voidwithFunctionBlock();voidnoLambda();};voidA::withNormalBlock(){try{throwstd::exception();}catch(...){[this](){std::coutinclang(好

C++ 相关名称 : Is this typename required?

在a.hpp中我定义了:#includenamespaceBoard{templatestructGroupNode{usingPointType=std::pair;//...};}然后,在b.cpp中我定义了:#include"a.hpp"namespaceBoard{templatestructNodeList{usingStdList=std::list>;}}//andthenuseNodeListnl;上面的代码可以在没有任何警告的情况下在gcc-6和clang-3.9上编译。但是,Clion2016.3提示cannotresolvevariableGroupNodeinb

c++ - 即使在 std::shared_ptr 拥有之后,shared_from_this 还是空的 _M_weak_this

我在A中存储了一个类(我们称它为std::vector)使用C++智能指针(因此vector签名为std::vector>)。#include#include#includeclassA:std::enable_shared_from_this{public:voiddoWork();std::shared_ptrgetSharedRef();};voidA::doWork(){std::coutA::getSharedRef(){returnshared_from_this();}classAManager{staticstd::vector>aList;public:staticv

c++ - 调用C++成员函数指针: this-pointer gets corrupted

我需要将一些成员函数指针转换为void*指针(因为我需要将它们压入Lua堆栈,但问题与Lua无关)。我使用union来做到这一点。但是,当我将成员函数指针转换为void*并再次返回,然后尝试使用该类的实例调用该指针时,this指针会损坏。奇怪的是,如果我将void*指针转换回C风格的函数指针,并将指向该类的指针作为第一个参数,这个问题就不会发生。这是演示问题的一段代码:#includeusingnamespacestd;classtest{inta;public:voidtellSomething(){coutworks//callwithCstylefunctionpointerin

c++ - "this"指针是否启用了 RTTI?

我试图在对象的继承树中的其中一个类的构造函数中发现对象的派生程度最高的类。我现在已经在这上面花了几个小时,并且不知道我还能怎么做或者为什么它没有意义。这似乎很有道理,但它拒绝工作。我找到了很多关于RTTI的页面,但基本上没有找到。我将在我的测试用例及其输出之后继续解释。来源:#include#include#includeclassA{public:A(std::stringfoo);virtualvoidbar(A*a)=0;};classB:publicA{public:B();virtualvoidbar(A*a);};A::A(std::stringfoo){std::cout

c++ - 在 C++11 中使用不带托管共享指针的 shared_from_this()

假设我有一个类是enable_shared_from_this的子类。这个基类的文档说在调用shared_from_this之前应该有一个拥有这个类的共享指针。使用new分配类并调用shared_from_this来管理对象是否安全? 最佳答案 正如其他用户已经提到的,在不属于shared_ptr的实例上调用shared_from_this将导致未定义的行为(通常是异常,但也有没有保证)。那么,为什么还要一个答案呢?因为我自己做了一次同样的问题并得到了几乎相同的答案,然后我开始为另一个问题而苦苦挣扎,这个问题紧随其后-我如何保证所有

c++ - 在 shared_from_this() 中 boost weak_ptr_cast

我正在使用boost的共享指针,并使用enable_shared_from_this来启用返回指向this的共享指针。代码如下所示:classfoo:publicboost::enable_shared_from_this{boost::shared_ptrget(){returnshared_from_this();}}为什么shared_from_this会抛出weak_ptr_cast异常? 最佳答案 如果您在堆栈上声明了foo,那么就没有其他指向foo的共享指针。例如:voidbar(){foofooby;fooby.get

c++ - boost shared_ptr 和 'this'

我有两个具有父子关系的类(客户&订单目录&文件等)我有typedefboost::shared_ptrParentPtr在父类中创建子类的方法我需要子实例有指向它们父实例的指针。classChild{....ParentPtrm_parent;....}我希望它是一个shared_ptr,这样在存在子项时父项不会消失。我还有其他人持有ParentPtrs给父级(Parents的工厂方法返回一个ParentPtr)问题:如何给child一个ParentPtr尝试(1)。在Parent::ChildFactorychild->m_parent.reset(this);这会导致非常糟糕的事情

c++ - 什么时候应该使用this->?

我想知道这个->是否应该同时使用:voidSomeClass::someFunc(intpowder){this->powder=powder;}//andvoidSomeClass::someFunc(boolenabled){this->isEnabled=enabled;}我想知道后者是否是正确的必要条件,或者isEnabled=enabled是否就足够了。谢谢 最佳答案 this->在直接使用成员会产生歧义时需要。这可能发生在模板代码中。考虑一下:#includetemplateclassFoo{public:Foo(){}