By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,903 Members | 1,184 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,903 IT Pros & Developers. It's quick & easy.

constructor syntax

P: 65
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?

Expand|Select|Wrap|Line Numbers
  1. class BadIndex      // exception class for indexing problems
  2.     {
  3.     private:
  4.         int badindex;   // problematic index value
  5.      public:
  6.         BadIndex(int i) : badindex(i) {}
  7.         virtual void Report() const;
  8.     };
May 29 '07 #1
Share this Question
Share on Google+
6 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
This syntax:
Expand|Select|Wrap|Line Numbers
  1. BadIndex(int i) : badindex(i) {}
  2.  
use the constructor initializer list. Here badindex is created and intialized to i before the constructor starts.

Whereas this syntax:
Expand|Select|Wrap|Line Numbers
  1. BadIndex(int i) {badindex=i; }
  2.  
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.

Expand|Select|Wrap|Line Numbers
  1. class AClass
  2. {
  3.        const int Max;
  4.     public:
  5.        AClass(int theValue);
  6. };
  7.  
  8. AClass::AClass(int theValue) : Max(theValue)
  9. {
  10.  
  11. }
  12.  

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.

Expand|Select|Wrap|Line Numbers
  1. class Base
  2. {
  3.        int theData;
  4.        public:
  5.           Base(int theValue);
  6. };
  7.  
  8. Base::Base(int theValue) : theData(theValue)
  9. {
  10.  
  11. }
  12.  
  13. class Derived
  14. {
  15.      public:
  16.         Derived(int theValue);
  17. };
  18.  
  19. Derived::Derived(int theValue) : Base(theValue) //calls Base ctor
  20. {
  21.  
  22. }
  23.  
};


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.

Expand|Select|Wrap|Line Numbers
  1. class Date
  2. {
  3.      int month;
  4.      int day;
  5.      int year;
  6.     public:
  7.        Date(int m, int d, int y);
  8. };
  9. Date::Date(int m, int d, int y) : month(m), day(d), year(d)
  10. {
  11.  
  12. }
  13.  
  14. class MyClass
  15. {
  16.      Date dt;
  17.  
  18.      public:
  19.        MyClass(int m, int d, int y);
  20. };
  21.  
  22. MyClass::MyClass(int m, int d, int y) : dt(m,d,y)   //call Date ctor
  23. {
  24.  
  25. }
  26.  
May 29 '07 #2

AdrianH
Expert 100+
P: 1,251
I agree with everything you said except this:
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
May 29 '07 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
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 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.
May 29 '07 #4

AdrianH
Expert 100+
P: 1,251
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.
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?
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
May 29 '07 #5

weaknessforcats
Expert Mod 5K+
P: 9,197
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.
May 29 '07 #6

AdrianH
Expert 100+
P: 1,251
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
May 29 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.