initializer list of copy constructor

Discussion in 'C++' started by ccs, Jun 6, 2004.

  1. ccs

    ccs Guest

    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!
     
    ccs, Jun 6, 2004
    #1
    1. Advertising

  2. "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
     
    Victor Bazarov, Jun 6, 2004
    #2
    1. Advertising

  3. ccs

    Rolf Magnus Guest

    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.
     
    Rolf Magnus, Jun 7, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Razvan
    Replies:
    7
    Views:
    17,641
    Lee Fesperman
    Jul 4, 2004
  2. Chris K
    Replies:
    1
    Views:
    580
    Victor Bazarov
    Apr 17, 2004
  3. Replies:
    5
    Views:
    10,276
    Rolf Magnus
    Apr 15, 2005
  4. Pallav singh
    Replies:
    5
    Views:
    660
  5. cinsk
    Replies:
    35
    Views:
    2,662
    James Kanze
    Oct 11, 2010
Loading...

Share This Page