"ccs" <> 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
|