Like Tree3Likes

Inheritance and construction question about members

This is a discussion on Inheritance and construction question about members within the C++ Programming forums, part of the General Programming Boards category; I for one would almost go as far as to say that initialisation list is always preferable over using the ...

  1. #16
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    I for one would almost go as far as to say that initialisation list is always preferable over using the function body.
    However there is one gotcha, the variables initialised in an initialisation list are actually initialised in the order that the variables are declared within the class, not merely in the order they appear in the initialisation list. Not knowing this can lead to writing incorrect code, where direct assignment statements would have worked.
    It is also worth noting that there are some things for which the initialisation list is the only option (i.e references) and there are some things for which assignment statements are the only option (i.e. initialising an array).

    Lastly, dare I blow your mind with function try blocks?!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  2. #17
    Registered User
    Join Date
    Mar 2008
    Posts
    10
    Extract from the book (The C++ programming language):

    Member initializers are essential for types for which initialization differs from assignment Ė that is, for member objects of classes without default constructors, for const members, and for reference members. For example:
    Code:
    class X {
         const int i;
         Club c;
         Club &pc;
         / / ...
         X(int ii, const string& n, Date d, Club& c): i(ii), c(n,d), pc(c) { }
    };
    There isnít any other way to initialize such members, and it is an error not to initialize objects of those types. For most types, however, the programmer has a choice between using an initializer and using an assignment. In that case, I usually prefer to use the member initializer syntax, thus making explicit the fact that initialization is being done. Often, there also is an efficiency advantage to using the initializer syntax. For example:
    Code:
    class Person{
         string name;
         string address;
         / / ...
         Person(const Person&);
         Person(const string& n, const string& a);
    };
    Person::Person(const string& n, const string& a): name(n){
         address = a;
    }
    Here name is initialized with a copy of n. On the other hand, address is first initialized to the empty string and then a copy of a is assigned.

    Regards.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Protected Members and Templated Inheritance
    By Axl in forum C++ Programming
    Replies: 6
    Last Post: 09-22-2011, 10:31 AM
  2. Data members in inheritance
    By DarthMaster84 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2010, 02:37 AM
  3. equal members in BST question..
    By transgalactic2 in forum C Programming
    Replies: 4
    Last Post: 10-30-2008, 01:32 PM
  4. static members construction
    By glUser3f in forum C++ Programming
    Replies: 3
    Last Post: 11-09-2003, 08:58 PM
  5. inheritance from classes own members?
    By atapi103 in forum C++ Programming
    Replies: 2
    Last Post: 09-02-2003, 10:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21