小编Lal*_*Box的帖子

在构造函数的初始化列表中使用“this”对 Qt 特别危险吗?

我需要关于“这个”主题的可靠信息:

class MyClass, public QWidget
{
public:
    MyClass( QWidget * parent = NULL )
         :QWidget( parent ),
         mpAnotherWidget( new QWidget( this ) ){};
private:
    QWidget * mpAnotherWidget;
};
Run Code Online (Sandbox Code Playgroud)

当然,在构造函数或初始化列表中调用虚函数是一个坏主意。问题是:这个代码可以吗

mpAnotherWidget( new QWidget( this ) )
Run Code Online (Sandbox Code Playgroud)

导致未定义的行为?!如果是这样:为什么?

如果可以,请引用您的来源!谢谢!

c++ qt constructor this-pointer initialization-list

6
推荐指数
1
解决办法
682
查看次数

标签 统计

c++ ×1

constructor ×1

initialization-list ×1

qt ×1

this-pointer ×1