构件对象的构造 [英] Construction of member objects

查看:56
本文介绍了构件对象的构造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否在构造函数体之前构造了成员对象?


请考虑以下示例。这是没关系的,或者是否可能在调用DoBar()之后创建_bar




class Bar

{

public:

Set(int i){_ i = i; }


私人:

int _i;

};


class Foo

{

public:

Foo(){DoBar(); }


private:

void DoBar(){_ bar.Set(10); }

Bar _bar;

};


提前致谢,

Joost


-----

人们的古代历史概念是星际迷航的第一个系列

可以耐心对待,因为通常不是他们的错,所以他们被减少为从学校接受教育。 - Terry Pratchett

Are member objects constructed before the body of the constructor executes?

Consider the following example. Is this okay or is it possible that _bar
will be created after the call to DoBar()?

class Bar
{
public:
Set(int i) { _i = i; }

private:
int _i;
};

class Foo
{
public:
Foo() { DoBar(); }

private:
void DoBar() { _bar.Set(10); }
Bar _bar;
};

Thanks in advance,
Joost

-----
"People whose concept of ancient history is the first series of Star Trek
may be treated with patience, because it''s usually not their fault they were
reduced to getting their education from school." -- Terry Pratchett

推荐答案

" Joost Ronkes Agerbeek" <乔*** @ ronkes.nl>写道:
"Joost Ronkes Agerbeek" <jo***@ronkes.nl> writes:
是否在构造函数体执行之前构造了成员对象?


是的。

请考虑以下示例。这是可以的,还是可能在调用DoBar()之后创建_bar



No.

班级酒吧
{
公开:
Set(int i){_ i = i; }

私人:
int _i;
};

类Foo
{
公开:
Foo (){DoBar(); }私有:
void DoBar(){_ bar.Set(10); }
Bar _bar;
};
Are member objects constructed before the body of the constructor
executes?
Yes.

Consider the following example. Is this okay or is it possible that _bar
will be created after the call to DoBar()?
No.

class Bar
{
public:
Set(int i) { _i = i; }

private:
int _i;
};

class Foo
{
public:
Foo() { DoBar(); }

private:
void DoBar() { _bar.Set(10); }
Bar _bar;
};



[snip]


[snip]




" llewelly" < 11 ********* @ xmission.dot.com>在消息中写道

news:86 ************ @ Zorthluthik.local.bar ...

"llewelly" <ll*********@xmission.dot.com> wrote in message
news:86************@Zorthluthik.local.bar...
" Joost Ronkes Agerbeek" <乔*** @ ronkes.nl>写道:
"Joost Ronkes Agerbeek" <jo***@ronkes.nl> writes:
在构造函数的主体执行之前是否构造了成员对象?
是的。
Are member objects constructed before the body of the constructor
executes?
Yes.

Consider the following example. Is this okay or is it possible that _bar
will be created after the call to DoBar()?



没有。



No.




Umm这是一个/或者问题。 :-)我认为他的意思是不:_bar不会在调用DoBar()之后创建
,但是已经由

时间创建了 。换句话说,是的,没关系。



Umm that was an either/or question. :-) I think he means "no: _bar will not
be created after the call to DoBar(), but will be already be created by that
time". In other words, yes, it''s ok.


班级酒吧
{
公开:
Set(int i) {_ i = i; }

私人:
int _i;
};

类Foo
{
公开:
Foo (){DoBar(); }私有:
void DoBar(){_ bar.Set(10); }
Bar _bar;
};

class Bar
{
public:
Set(int i) { _i = i; }

private:
int _i;
};

class Foo
{
public:
Foo() { DoBar(); }

private:
void DoBar() { _bar.Set(10); }
Bar _bar;
};


[snip]


[snip]



Howard写道:
llewelly < 11 ********* @ xmission.dot.com>在消息中写道
新闻:86 ************ @ Zorthluthik.local.bar ...
"llewelly" <ll*********@xmission.dot.com> wrote in message
news:86************@Zorthluthik.local.bar...
" Joost Ronkes Agerbeek" <乔*** @ ronkes.nl>写道:

"Joost Ronkes Agerbeek" <jo***@ronkes.nl> writes:

在构造函数的主体执行之前是否构造了成员对象?
Are member objects constructed before the body of the constructor
executes?



是的。 />



Yes.

请考虑以下示例。这是否可以,或者在调用DoBar()之后是否可以创建_bar
Consider the following example. Is this okay or is it possible that _bar
will be created after the call to DoBar()?



否。



No.



嗯这是一个问题。 :-)我认为他的意思是no:_bar不会在调用DoBar()之后创建,但是已经被那个
时间创建了。换句话说,是的,没关系。


Umm that was an either/or question. :-) I think he means "no: _bar will not
be created after the call to DoBar(), but will be already be created by that
time". In other words, yes, it''s ok.

class Bar
{
public:
Set(int i){_ i = i; }

私人:
int _i;
};

类Foo
{
公开:
Foo (){DoBar(); }私有:
void DoBar(){_ bar.Set(10); }
栏_栏;
};
class Bar
{
public:
Set(int i) { _i = i; }

private:
int _i;
};

class Foo
{
public:
Foo() { DoBar(); }

private:
void DoBar() { _bar.Set(10); }
Bar _bar;
};



[snip]



[snip]




声明含糊不清:创建Foo后立即创建_bar,并在输入Foo()体之前创建
。当调用DoBar()时,_bar
已经创建了
(其成员_i未初始化)。因此,调用

DoBar,然后调用Bar :: Set,就可以了。


Victor



The statement is ambiguous: _bar is created as soon as Foo is created and
before the body of Foo() is entered. By the time DoBar() is called, _bar
has already been created (with its member _i uninitialised). So, calling
"DoBar", which in turn calls Bar::Set, is OK.

Victor


这篇关于构件对象的构造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆