Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > initializer list of copy constructor

Reply
Thread Tools

initializer list of copy constructor

 
 
ccs
Guest
Posts: n/a
 
      06-06-2004
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!


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      06-06-2004
"ccs" <(E-Mail Removed)> wrote...
> 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


 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      06-07-2004
ccs wrote:

> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
template copy constructor vs normal copy constructor cinsk C++ 35 10-10-2010 11:14 PM
initializer Vs assignment list in constructor Pallav singh C++ 5 01-18-2008 06:13 PM
constructor initializer list jrefactors@hotmail.com C++ 5 04-15-2005 03:47 PM
No constructor initializer list in Java ? Razvan Java 7 07-04-2004 02:28 AM
composition/aggregation: would like to use constructor body rather than initializer list Chris K C++ 1 04-17-2004 08:38 PM



Advertisments