草庐IT

this_call

全部标签

c++ - 在构造函数的初始化列表中使用 "this"对 Qt 特别危险吗?

我需要关于“这个”主题的可靠信息:classMyClass,publicQWidget{public:MyClass(QWidget*parent=NULL):QWidget(parent),mpAnotherWidget(newQWidget(this)){};private:QWidget*mpAnotherWidget;};当然,在构造器或初始化列表中调用虚函数不是一个好主意。问题是:这段代码可以吗mpAnotherWidget(newQWidget(this))导致未定义的行为?!如果是这样:为什么?如果可以,请引用您的消息来源!谢谢! 最佳答案

c++ - "If you' 已经写了一个编译测试,你've written a call to main"

在程序中调用mainviolatesC++标准voidf(){main();//anendlessloopcallingmain?Nothat'snotallowed}intmain(){staticint=0;std::cout在lecture中ChandlerCarruth,大约在“22.40”说ifyou'vewrittenacompilertestyou'vewrittenacalltomain这有什么关系,或者如何克服标准不允许的事实? 最佳答案 这里的要点是,如果你编写编译器测试代码,你可能会想用一些不同的参数集测试调用

c++ - 错误 : 'object' was not declared in this scope

我是C++的新手,正在尝试制作大富翁游戏。不幸的是,它仍然显示两个类之间的声明错误。我已经尝试了所有方法,但真的不知道问题出在哪里。错误:“玩家”未在此范围内声明。引擎.h#ifndefENGINE_H#defineENGINE_H#include"Player.h"#includeusingnamespacestd;classEngine{public:Engine();//methodthatstartswithgame,takerandomnumberforgettingnumberofplayers,setplayerstovectorvoidplay();//methodwh

c++ - 为什么 "this"指针用于调用派生成员函数?

在this上阅读关于虚函数的教程(与本例无关)链接,我找到了这段代码。classWeapon{public:voidfeatures(){coutWeapon::features();cout类Weapon派生自类Bomb,其中调用了Weapon的成员函数。为什么使用“this”指针调用函数Weapon::features()?这不是已经隐式给出了吗? 最佳答案 this是隐式给出的,是否显式编写通常是风格问题。在您的情况下,我会说它不会提高可读性。然而,在其他情况下,显式写入this是有意义的,甚至是必要的为了避免局部变量和数据成

c++ - BOOST_FOREACH : What is the error on using this of a STL container?

有谁知道为什么以下会在VC9上产生错误?classElem;classElemVec:publicvector{public:voidfoo();};voidElemVec::foo(){BOOST_FOREACH(Elem&elem,*this){//Dosomethingwithelem}return;}我得到的错误是:errorC2355:'this':canonlybereferencedinsidenon-staticmemberfunctions我现在拥有的唯一(hack)解决方案是:voidElemVec::foo(){ElemVec*This=this;BOOST_FO

c++ - 模板 :Name resolution:Point of instantiation: -->can any one tell some more examples for this statement?

这是来自ISOC++标准14.6.4.1实例化点的声明Forafunctiontemplatespecialization,amemberfunctiontemplatespecialization,oraspecializationforamemberfunctionorstaticdatamemberofaclasstemplate,ifthespecializationisimplicitlyinstantiatedbecauseitisreferencedfromwithinanothertemplatespecializationandthecontextfromwhichi

c++ - 具有固定实现的虚函数不使用 (*this) 的大多数派生类

假设我有以下代码:structZ;structA{virtualvoidDo(Z&z)const;};structB:publicA{};structZ{voiduse(Aconst&a){}voiduse(Bconst&b){}};voidA::Do(Z&z)const{z.use(*this);}现在,当我调用B.do,this的类型是A,这是有道理的,因为do的实现在A中定义.有什么方法可以调用B.do使用use(Bconst&)无需为do复制粘贴相同的代码来自A进入B?在我的实际代码中,我有大约15个(并且还在不断增加)派生自某个基类的类,必须为do复制粘贴相同的代码似乎很浪费

c++ - 在基类中调用 shared_from_this() 时的 bad_weak_ptr

我有一个SuperParent类,一个Parent类(派生自SuperParent)并且都包含一个shared_ptr到一个Child类(它包含一个weak_ptr到一个SuperParent)。不幸的是,我在尝试设置Child的指针时遇到了bad_weak_ptr异常。代码如下:#include#include#include#includeusingnamespaceboost;classSuperParent;classChild{public:voidSetParent(shared_ptrparent){parent_=parent;}private:weak_ptrpare

c++ - 构造函数中抛出的异常 : is the destructor called?

这个问题在这里已经有了答案:Whatdestructorsarerunwhentheconstructorthrowsanexception?(3个答案)关闭8年前。如果在对象的构造函数中抛出异常,那么是否会调用析构函数?还是未定义的行为?(这就是为什么我不愿意说出我的编译器做了什么。)structfoo(){foo(){throw"bar";}~foo(){/*amIcalled*/}};foof;

c++ - 在 Visual Studio 中调用 std::swap 时的 std::bad_function_call

我正在尝试将我的代码从Linux移植到Windows。但是,对于VisualStudio,我的代码因以下错误而崩溃:MicrosoftC++exception:std::bad_function_callatmemorylocation这是我的代码:#includeclassFoo{public:Foo(int):m_deleter{[](){}}{}Foo(constFoo&)=delete;Foo(Foo&&)=default;Foo&operator=(constFoo&)=delete;Foo&operator=(Foo&&)=default;~Foo(){m_deleter(