constructor initializer list

Discussion in 'C++' started by jrefactors@hotmail.com, Apr 15, 2005.

  1. Guest

    I don't understand the usage of constructor initializer list
    and when must it be used. I always do code 1 and never do code 2,
    but what's the difference between them, and when to use which.

    class Student
    {
    public:
    Student();

    private:
    int _id;
    string _name;
    };

    //code 1
    Student::Student(int id, String name)
    {
    _id = id;
    _name = name;
    }

    //code 2
    Student::Student(int id, String name)
    : _id(id),
    _name(name)
    {
    }

    Please advise. thanks!!



    In C++, what are the differences between a class and a struct?
     
    , Apr 15, 2005
    #1
    1. Advertising

  2. wrote:
    > I don't understand the usage of constructor initializer list
    > and when must it be used. I always do code 1 and never do code 2,
    > but what's the difference between them, and when to use which.
    > [...]
    >
    > In C++, what are the differences between a class and a struct?


    Both topics are covered in the FAQ. Please take a read. You can
    find the FAQ here: http://www.parashift.com/c -faq-lite/
     
    Victor Bazarov, Apr 15, 2005
    #2
    1. Advertising

  3. Rolf Magnus Guest

    wrote:

    > I don't understand the usage of constructor initializer list
    > and when must it be used. I always do code 1 and never do code 2,
    > but what's the difference between them, and when to use which.


    Always use version two if you can. It will initialize the objects with the
    parameters you specified, while version 1 will first default-initialize the
    object, then assign the provided value to it. In your example, it doesn't
    make much of a difference, but for classes that have expensive constructors
    or assignment operators, 2 is more efficient. Also, you cannot use verison
    2 for const or reference members, since those cannot be assigned to.

    > class Student
    > {
    > public:
    > Student();
    >
    > private:
    > int _id;
    > string _name;
    > };
    >
    > //code 1
    > Student::Student(int id, String name)
    > {
    > _id = id;
    > _name = name;
    > }
    >
    > //code 2
    > Student::Student(int id, String name)
    > : _id(id),
    > _name(name)
    > {
    > }
    >
    > Please advise. thanks!!
    >
    >
    >
    > In C++, what are the differences between a class and a struct?


    The default access permissions and inheritance are private for classes and
    public for structs.
     
    Rolf Magnus, Apr 15, 2005
    #3
  4. Guest

    and that default-initialization mentioned by Rolf will simply fail to
    compile in the first version if any of your class members is of a type
    that has no default constructor.
     
    , Apr 15, 2005
    #4
  5. Guest

    and that default-initialization mentioned by Rolf will simply fail to
    compile in the first version if any of your class members is of a type
    that has no default constructor.
     
    , Apr 15, 2005
    #5
  6. Rolf Magnus Guest

    Rolf Magnus wrote:

    > Also, you cannot use verison 2 for const or reference members, since those
    > cannot be assigned to.


    Of course that should have been:

    Also, you cannot use version 1 for const or reference members, since those
    cannot be assigned to.

    >> class Student
    >> {
    >> public:
    >> Student();
    >>
    >> private:
    >> int _id;
    >> string _name;
    >> };
    >>
    >> //code 1
    >> Student::Student(int id, String name)
    >> {
    >> _id = id;
    >> _name = name;
    >> }
    >>
    >> //code 2
    >> Student::Student(int id, String name)
    >> : _id(id),
    >> _name(name)
    >> {
    >> }
     
    Rolf Magnus, Apr 15, 2005
    #6
    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,577
    Lee Fesperman
    Jul 4, 2004
  2. Chris K
    Replies:
    1
    Views:
    572
    Victor Bazarov
    Apr 17, 2004
  3. ccs
    Replies:
    2
    Views:
    395
    Rolf Magnus
    Jun 7, 2004
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,244
  5. Pallav singh
    Replies:
    5
    Views:
    646
Loading...

Share This Page