constructor syntax

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stmfc
    New Member
    • May 2007
    • 65

    constructor syntax

    i have the code fragment below,
    is there anybody to explain me the meaning of this syntax:
    BadIndex(int i) : badindex(i) {}

    is it the same with this:
    BadIndex(int i) {badindex=i; }
    if it is the same why the former one is used instead of the latter?

    [code=cpp]class BadIndex // exception class for indexing problems
    {
    private:
    int badindex; // problematic index value
    public:
    BadIndex(int i) : badindex(i) {}
    virtual void Report() const;
    };[/code]
    Last edited by AdrianH; May 29 '07, 02:46 PM. Reason: Please use [code=cpp][/code] tags to improve readability.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This syntax:
    [code=cpp]
    BadIndex(int i) : badindex(i) {}
    [/code]

    use the constructor initializer list. Here badindex is created and intialized to i before the constructor starts.

    Whereas this syntax:
    [code=cpp]
    BadIndex(int i) {badindex=i; }
    [/code]

    says to change the value of the existing badindex and make it equal to i.

    Remember, when you get to the { of your constructor, the class member variables have already been created and initialized using the default constructors of the member variables. All you can do inside the BadIndex constructor is change things.

    The two syntaxes are not equivalent. If the class has a const member, C++ requires that the const have a value at the time the const is created. By the time you reach the { of the constructor, the const member has already been created. You need a way to get a value to the const before the constructor starts. Here is where you use the initializer list.

    [code=cpp]
    class AClass
    {
    const int Max;
    public:
    AClass(int theValue);
    };

    AClass::AClass( int theValue) : Max(theValue)
    {

    }
    [/code]


    A second difficulty arises when you use inheritance. C++ requires the base class portion of your derived object to be initialized before the derived constructor starts. Here, again, you use the initializer list to call the base class constructor.

    [code=cpp]
    class Base
    {
    int theData;
    public:
    Base(int theValue);
    };

    Base::Base(int theValue) : theData(theValu e)
    {

    }

    class Derived
    {
    public:
    Derived(int theValue);
    };

    Derived::Derive d(int theValue) : Base(theValue) //calls Base ctor
    {

    }
    [/code]
    };


    A third difficulty arises when one of your member classes is the object of some other unrelated class and you need to call a constructor of that class becuse that class does not have a default constructor. Again, use the initializer list for this.

    [code=cpp]
    class Date
    {
    int month;
    int day;
    int year;
    public:
    Date(int m, int d, int y);
    };
    Date::Date(int m, int d, int y) : month(m), day(d), year(d)
    {

    }

    class MyClass
    {
    Date dt;

    public:
    MyClass(int m, int d, int y);
    };

    MyClass::MyClas s(int m, int d, int y) : dt(m,d,y) //call Date ctor
    {

    }
    [/code]
    Last edited by weaknessforcats; May 29 '07, 03:11 PM. Reason: Fix error in code sample

    Comment

    • AdrianH
      Recognized Expert Top Contributor
      • Feb 2007
      • 1251

      #3
      I agree with everything you said except this:
      Originally posted by weaknessforcats
      Remember, when you get to the { of your constructor, the class member variables have already been created and initialized using the default constructors of the member variables. All you can do inside the BadIndex constructor is change things.
      The initialiser list is not the default constructor. It is just a list of initialisers that state the initial state of the class prior to executing the constructor's body. I think the reasoning is for increased efficiency of the constructor, and of course everything else you stated.

      The basic steps are:
      1. Allocate the object.
      2. Call constructor which does:
        1. Calls base class constructor from initialiser list (if derived from a class).
        2. Init rest of member variables in the initialiser list in the order they appear in the class NOT the order they appear in the initialiser list (I’m not sure ordering is part of the standard or just gnu’s implementation) .
        3. Execute constructor’s body.



      Adrian

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        You are mostly correct. I have modified this quote below so that it is accurate

        Originally posted by AdrianH
        The initialiser list is not the default constructor. It is just a list of initialisers that state the initial state of the class prior to executing the constructor's body. I think the reasoning is for increased efficiency of the constructor, and of course everything else you stated.

        The basic steps are:
        Allocate the object.
        Call constructor which does:
        Calls base class constructor from initialiser list (if derived from a class).
        Init rest of member variables in the initialiser list in the order they appear in the class NOT the order they appear in the initialiser list (I’m not sure ordering is part of the standard or just gnu’s implementation) .
        Execute constructor’s body.
        The initialiser list is not the default constructor. It is just a list of initialisers that provde the initial state of the class members. If the class member is an object of another class, that other class constructor must be called to initialize the member. When the class constructor starts at the {, all of the class data members have already been allocated and their constructors have already been called.




        The basic steps are:

        Allocate the object member-by-member as they are declared in the class

        Call the constructor on each member before allocating the next member.
        The correct constructor to call is either a) the default constructor or b) the constructor specified in the initializer list. This ordering is very important. For example, the third member may need the value of the second member in order to initialize itself. Note that fundamental types and pointers do not have a default initial value or a constructor.

        Call the class constructor to change the values of any members that are fundamental types or pointers or have not been initialized as yet for whatever reason.

        When inheritance is involved, the basic steps above are followed first for the base class and then for the derived class. The initializer list is the only method for the derived class constructor to specify which base class constructor to call.

        Comment

        • AdrianH
          Recognized Expert Top Contributor
          • Feb 2007
          • 1251

          #5
          Originally posted by weaknessforcats
          You are mostly correct. I have modified this quote below so that it is accurate



          The initialiser list is not the default constructor. It is just a list of initialisers that provde the initial state of the class members. If the class member is an object of another class, that other class constructor must be called to initialize the member. When the class constructor starts at the {, all of the class data members have already been allocated and their constructors have already been called.




          The basic steps are:

          Allocate the object member-by-member as they are declared in the class

          Call the constructor on each member before allocating the next member.
          The correct constructor to call is either a) the default constructor or b) the constructor specified in the initializer list. This ordering is very important. For example, the third member may need the value of the second member in order to initialize itself. Note that fundamental types and pointers do not have a default initial value or a constructor.
          Agreed ordering is very important, that is why I mentioned it in point 2.2.
          Originally posted by weaknessforcats
          Call the class constructor to change the values of any members that are fundamental types or pointers or have not been initialized as yet for whatever reason.
          Are you referring to point 2.3?
          Originally posted by weaknessforcats
          When inheritance is involved, the basic steps above are followed first for the base class and then for the derived class. The initializer list is the only method for the derived class constructor to specify which base class constructor to call.
          That would be point 2.1.

          I’ve updated my steps to be more detailed. Do these changes make it accurate?

          The basic steps are:
          1. Allocate the object.

          2. Call constructor which does:

            1. Calls base class constructor from initialiser list (if derived from a class).

              This can be the base class’s default constructor if you didn’t specify which or one of your choosing.

            2. Init rest of member variables as stated in the initialiser list in the order they appear in the class NOT the order they appear in the initialiser list (I’m not sure ordering is part of the standard or just gnu’s implementation) .

              If you do not explicitly initialise a member variable, then it is initialise it with its default constructor or if it doesn’t have one but does have members that have constructors (and so on) then initialise them in the order they appear in the class.

              If the member variable has no constructor or any of its members have no constructor (and so on) then do nothing with it. Its initial value is undefined.


            3. Execute constructor’s body.



          Adrian

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Originally posted by AdrianH
            If the member variable has no constructor or any of its members have no constructor (and so on) then do nothing with it. Its initial value is undefined.
            Looks good except for the above.

            If the member variable is a an instance of a class, then you will die with a compile time error for no appropriate constructor.


            If the member variable is a fundamental type or a pointer, then your statement is correct.

            Comment

            • AdrianH
              Recognized Expert Top Contributor
              • Feb 2007
              • 1251

              #7
              Originally posted by weaknessforcats
              Looks good except for the above.

              If the member variable is a an instance of a class, then you will die with a compile time error for no appropriate constructor.


              If the member variable is a fundamental type or a pointer, then your statement is correct.
              I don't believe you are correct. Can you create a bit of sample code to show this property?


              Adrian

              Comment

              Working...