initializer list of copy constructor

C

ccs

First, no compiling error for the following code...

class CStudent
{
int id;
public:
CStudent(int i) : id(i);
};

class CTeam
{
CStudent s;
public:
CTeam(int i) : s(i) {}
CTeam(const CStudent& c) : s(c.s);
};

Is the code above correct to use intializer list for copy constructor?

Is it correct to have different initializer lists for CTeam(int i) and
CTeam(const CStudent& c)?

For statement "CTeam(const CStudent& c) : s(c.s);", is the default copy
constructor of CStudent triggered?

Is it necessary to define a copy constructor for CStudent to replace its
default one?


Thanks in advance!
 
V

Victor Bazarov

ccs said:
First, no compiling error for the following code...

class CStudent
{
int id;
public:
CStudent(int i) : id(i);

Really? No error here? What compiler?
};

class CTeam
{
CStudent s;
public:
CTeam(int i) : s(i) {}
CTeam(const CStudent& c) : s(c.s);

And here too? Really?! I somehow can't belive that.
};

Is the code above correct to use intializer list for copy constructor?

No, of course not. You cannot have an initialiser list in a declaration.
The correct way is to use an initialiser list in a definition:

CTeam(CStudent const &c) : s(c.s) {}

(notice the curly braces after the initialiser list).
Is it correct to have different initializer lists for CTeam(int i) and
CTeam(const CStudent& c)?

Sure. Whatever is dicated by your design.
For statement "CTeam(const CStudent& c) : s(c.s);", is the default copy
constructor of CStudent triggered?

The statement you're quoting here is a syntax error. If you have the
copy c-tor for CTeam as I recommended, then yes, the compiler-defined
copy c-tor for 'CStudent' will be used to construct the 's' member.
Is it necessary to define a copy constructor for CStudent to replace its
default one?

No.

Victor
 
R

Rolf Magnus

ccs said:
First, no compiling error for the following code...

class CStudent
{
int id;
public:
CStudent(int i) : id(i);

This should actually procuce an error message. Your constructor is
missing a body.
};

class CTeam
{
CStudent s;
public:
CTeam(int i) : s(i) {}
CTeam(const CStudent& c) : s(c.s);

Same here.
};

Is the code above correct to use intializer list for copy constructor?

No. Even if you have an initalizer list, the constructor needs a body.
Is it correct to have different initializer lists for CTeam(int i) and
CTeam(const CStudent& c)?

Sure. What would they be good for if that weren't the case?
For statement "CTeam(const CStudent& c) : s(c.s);", is the default
copy constructor of CStudent triggered?
Yes.

Is it necessary to define a copy constructor for CStudent to replace
its default one?

In the above example, no.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top