初始化列表中用到this,有什么后果

jeng 2006-09-08 08:09:16
#include <iostream>
using namespace std;

class Ctrl;
class Bar;

class Par
{
int tr;
};
class Ctrl
{
int theAge;
Bar* pBar;
public:
Ctrl(Bar* pbar);
};

class Bar: public Par
{
Ctrl ctrl;
public:
Bar();
};

Bar::Bar():ctrl(this)
{

}


Ctrl::Ctrl(Bar* pbar):theAge(2),pBar(pbar)
{

}
////////////////
int main()
{
Bar bar;

return 0;
}

我不想用new来让自己管理对象,我也不想用一个SetBar()来设定parent对象,但我想知道Ctrl包含在谁的里面,所以我用初始化列表,便以的时候提示:warning C4355: 'this' : used in base member initializer list

Bar对象还没有构造好,因此this指针可能有问题!
调试没有问题!但我觉得的还是不保险!
...全文
207 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
jeng 2006-09-12
  • 打赏
  • 举报
回复
连续好几天都遇到这个问题,我只能改成new!
jeng 2006-09-09
  • 打赏
  • 举报
回复
ding!

15,975

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧