473,396 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Construction of member objects

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
9 1345
"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

"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
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
int _i;

Let me please just take this opportunity to express my disgust at your
coding style.
-JKop
Jul 22 '05 #5
Thanks everyone, for clearing that up.
Jul 22 '05 #6
:-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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jerivix Entadi | last post by:
I'm attempting to create an application to work with a fluid database of people. I'm doing this in a command line, text-based manner. I need to know two things: 1) How do I save data, perhaps a...
2
by: newbiecpp | last post by:
I have a simple class: class Point { public: Point() : xval(0), yval(0) {} Point(int x, int y) : xval(x), yval(0) {} private: int xval, yval; };
14
by: trying_to_learn | last post by:
i am on the chapter on copy construction in C++ in the code (see below), the author says if u pass the object by value as in HowMany h2 = f(h); ....then a bitwise object is created w/o calling...
7
by: Dave | last post by:
Hello all, In the code below, I use a pointer to an object under construction. Is the usage below legal? I have come across similar code at work. It compiles, but I'm not sure it's really...
11
by: bluekite2000 | last post by:
I have a Matrix class derived from an Array class as followed: Matrix(int nM, int nN) :Array<T>(nM*nN),mnM(nM),mnN(nN) { } However, I dont want to call Array constructor if nM!=nN. In other...
15
by: Jakob Bieling | last post by:
Hi, I am aware of the fact, that the order of construction of global objects is unspecified. But I am in a situation, where I need to guarantee, that one object is created before others (not all...
3
by: MKoool | last post by:
Hi everyone, I am doing several operations on lists and I am wondering if python has anything built in to get every member of several objects that are in an array, for example, if i have a class...
1
by: Brian Richards | last post by:
I have an object that I'm serializing (binary) that contains other internal objects that are also serialized. The Object contains two other member objects (let's call them source, and depend) one...
9
by: Ulrich Hobelmann | last post by:
Hi, slowly transitioning from C to C++, I decided to remodel a struct/union (i.e. type identifier as first field, union of variant types) as a class + subclasses. Switching functions are replaced...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.