inner class and initialization list

S

Scott

Hi All,

I have two classes like so:

class A
{
// ...stuff
};

class B
{
public:
B() {}

private:
A a;
};

How can I specify that B should initialize a with A's default constructor
in its own initialization list. For example, do I write:

class B
{
public:
B() : a(a()) {}
// ..etc

or just,

B() : a() {}

or is there some other way?

Thanks for any assistance!

Best,
Scott
 
M

mlimber

Scott said:
Hi All,

I have two classes like so:

class A
{
// ...stuff
};

class B
{
public:
B() {}

private:
A a;
};

How can I specify that B should initialize a with A's default constructor
in its own initialization list. For example, do I write:

class B
{
public:
B() : a(a()) {}
// ..etc

or just,

B() : a() {}

or is there some other way?

a(A()) would work (note the change in case), a() is preferable, and
leaving out altogether will accomplish the same thing.

Cheers! --M
 
S

Specktowsky Specktowsky

Scott,
it is not necesary specify in the initialization list the default
constructor of member classes (a). The 'run-time' call default
constructors of all member classes before the constructor of the main
class.
Take care.
 

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,780
Messages
2,569,611
Members
45,278
Latest member
BuzzDefenderpro

Latest Threads

Top