草庐IT

this-reference

全部标签

c++ - 我可以使用 memcpy(*this) 修改构造函数中的所有类成员吗?

structSomething{inta;intb;Something(char*buffer){memcpy(this,buffer,sizeof(Something));};};这合法吗?安全的?对我来说它看起来不错,但我不确定C++标准是否以某种方式禁止它。 最佳答案 ...fromthefactthatit'snolongeraPODtypeafterIaddedtheconstructor.这不是事实(只是假新闻;-))。添加构造函数不会更改struct的POD类型状态。您还可以使用static_assert轻松检查:st

C++ 对 WinMain@16 的 undefined reference (Code::Blocks)

我正在使用Code::Blocks学习C++,每次我尝试创建一个新类时,我都会收到一条错误消息:undefinedreferenceto`WinMain@16'这是我一直在使用的代码:主类#include"Lime.h"#includeusingnamespacestd;intmain(){Limelime;return0;}青柠类(.ccp):#include"Lime.h"#includeusingnamespacestd;Lime::Lime(){cout石灰header(.h):#ifndefLIME_H#defineLIME_HclassLime{public:Lime();

c++ - std::condition_variable::wait_until 相对于 std::this_thread::sleep_for 有什么优势吗?

在时间等待场景中:oursoftwareworksinthebackground,andsynchronizesdatawiththeserverinevery20-30minutes.我想用std::this_thread::sleep_for但我的上级强烈反对任何形式的sleep功能。他推荐std::condition_variable::wait_until(lock,timeout-time,pred)不知道在这种情况下sleep_for有什么缺点吗? 最佳答案 正如评论中已经指出的那样,这仅取决于您的用例。两者之间的主要区

c++ - 提升.Python : Getting a python weak reference to a wrapped C++ object

我已经使用Boost.Python包装了一个C++类。这些对象在C++端具有强引用(boost::shared_ptr),并且在Python中也可能存在断断续续的强引用。到目前为止,一切正常。但是,如果我从其中一个强引用创建一个python弱引用,那么一旦最后一个python强引用消失,这个弱引用就会被删除。我希望弱引用一直存在,直到C++端的最后一个强引用也消失为止。有可能实现吗?换句话说:有没有办法从python中找出特定的C++对象(由Boost.Python包装)是否仍然存在? 最佳答案 您如何持有对包装类的“C++强引用”

c++ - this指针是如何捕获的?

考虑以下代码:structS{intx;voidf(){autol=[&](){x=42;};//thisisimplicitlycapturedhere}};§5.1.2/14指出:Anentityiscapturedbycopyifitisimplicitlycapturedandthecapture-defaultis=orifitisexplicitlycapturedwithacapturethatdoesnotincludean&.因此我得出结论,this不是通过复制捕获。但随后通过§5.1.2/15:Anentityiscapturedbyreferenceifitisi

c++ - 如何在没有多余 RC 操作的情况下在派生类中使用 shared_from_this?

如果我想创建一个shared_ptr在从基类继承的层次结构中的派生类成员函数中,我可以使用shared_from_this和static_pointer_cast:classBase:publicstd::enable_shared_from_this{};classDer:publicBase{public:std::shared_ptrmake_SP_to_Me(){returnstd::static_pointer_cast(shared_from_this());}};我担心的是static_pointer_cast通过lvalue-ref-to-const接受它的参数,所以当

c++ - 非类型模板参数 : how to pass reference to base class object?

似乎无法传递对的引用派生对象的基类对象作为模板参数,正如我在这里尝试做的那样:structa{int_v;constexpra():_v(0){}constexpra(intv):_v(v){}};structc:publica{constexprc():a(){}constexprc(intv):a(v){}};externconstcdefault_a;constexprconstcdefault_a{1};consta&c_as_a=default_a;//^--thisline(16)causesnoerror-ccanbeconvertedtoatemplatestructb

c++ - 在括号内的 lambda 中声明名称为 `this` 的变量会在 3 个不同的编译器上产生不同的结果

在C++中,可以在括号内声明变量,例如int(x)=0;。但似乎如果您使用this而不是变量名,则使用构造函数代替:A(this);调用A::A(B*)。那么第一个问题就是为什么this不一样,是不是因为变量不能命名为this?让事情变得复杂一点,让我们把this放在lambda中-structB;structA{A(B*){}};structB{B(){[this]{A(this);}();}};现在gcc调用A::A(B*),msvc打印关于缺少默认构造函数的错误,clang打印expectedexpression(https://godbolt.org/g/Vxe0fF)。它在m

c++ - 关键字 `this` 可以在类范围内使用吗?

看来我可以在类范围内定义捕获this的lambda表达式。据我阅读N4640最新的工作草案,我找不到允许该行为的句子。我想我错过了什么......这是一个例子:#include#includestructfoo{std::functionf1=[this]{++i;};inti=0;};intmain(){fooa;fooconst&cref=a;cref.f1();std::cout运行演示。(g++-std=c++11迂腐)https://wandbox.org/permlink/HPzaOxbBkOQOmuS6已更新感谢@Brian和@cpplerner的评论,我明白了我的基本问

c++ - 来自同一个 enable_shared_from_this 实例的两个 shared_ptr

鉴于这个类是enable_shared_from_this:classconnection:publicstd::enable_shared_from_this{//...};假设我从sameconnection*创建了两个std::shared_ptr实例,如下所示:std::shared_ptrrc(newconnection);std::shared_ptrfc(rc.get(),[](connectionconst*c){std::cout到目前为止一切正常,因为资源{connection*}由单个shared_ptr—rc准确地说,fc只是有一个假的删除器。之后,我这样做:a