By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 1,005 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

Construction of member objects

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
"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.

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]
Jul 22 '05 #2

P: n/a

"llewelly" <ll*********@xmission.dot.com> wrote in message
news:86************@Zorthluthik.local.bar...
"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 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; }

private:
int _i;
};

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

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

[snip]

Jul 22 '05 #3

P: n/a
Howard wrote:
"llewelly" <ll*********@xmission.dot.com> wrote in message
news:86************@Zorthluthik.local.bar...
"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 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; }

private:
int _i;
};

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

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


[snip]


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
Jul 22 '05 #4

P: n/a
int _i;

Let me please just take this opportunity to express my disgust at your
coding style.
-JKop
Jul 22 '05 #5

P: n/a
Thanks everyone, for clearing that up.
Jul 22 '05 #6

P: n/a
:-D Any suggestions for how I should clean up my act?

Joost
"JKop" <NU**@NULL.NULL> wrote in message
news:97******************@news.indigo.ie...
int _i;

Let me please just take this opportunity to express my disgust at your
coding style.
-JKop

Jul 22 '05 #7

P: n/a
Joost Ronkes Agerbeek wrote:
:-D Any suggestions for how I should clean up my act?

Joost
"JKop" <NU**@NULL.NULL> wrote in message
news:97******************@news.indigo.ie...
int _i;

Let me please just take this opportunity to express my disgust at your
coding style.
-JKop


One solution that may be more to his liking, would be to change your
class as such:

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

private:
int i;
};

Alan
Jul 22 '05 #8

P: n/a
Joost Ronkes Agerbeek posted:
:-D Any suggestions for how I should clean up my act?

Joost
"JKop" <NU**@NULL.NULL> wrote in message
news:97******************@news.indigo.ie...
> int _i;

Let me please just take this opportunity to express my disgust at your
coding style.
-JKop

Well, here's what *I* might do: Name the member variable:

m_Chocolate
And name the variable passed to a function:

Chocolate
And for global variables:

g_Chocolate
And for static member variables:

ClassName::s_Chocolate
And for a pointer:

pChocolate
-JKop

Jul 22 '05 #9

P: n/a
JKop wrote:
Joost Ronkes Agerbeek posted:

:-D Any suggestions for how I should clean up my act?

Joost
"JKop" <NU**@NULL.NULL> wrote in message
news:97******************@news.indigo.ie...
int _i;
Let me please just take this opportunity to express my disgust at your
coding style.
-JKop


Well, here's what *I* might do: Name the member variable:

m_Chocolate
And name the variable passed to a function:

Chocolate
And for global variables:

g_Chocolate
And for static member variables:

ClassName::s_Chocolate
And for a pointer:

pChocolate
-JKop


Let me please just take this opportunity to express my disgust at your
coding style.
Jul 22 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.