Construction of member objects

  • Thread starter Joost Ronkes Agerbeek
  • Start date
J

Joost Ronkes Agerbeek

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
 
L

llewelly

Joost Ronkes Agerbeek said:
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]
 
H

Howard

llewelly said:

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]
 
V

Victor Bazarov

Howard said:


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
 
A

Alan Johnson

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

Joost

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
 
J

JKop

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

Joost


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
 
J

Jeff Schwab

JKop said:
Joost Ronkes Agerbeek posted:





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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top