Thread: member variable initialization

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    member variable initialization

    Hello everyone,


    I am wondering in the following code, member variable a in class B is not put in the initialization list or constructor of B directly, but it is initialized. How and when member variable a of class B is created and initialized? Is constructor of B invokes constructor of a?

    Output is,

    In constructor A
    In constructor B

    Code:
    #include <iostream>
    
    using namespace std;
    
    class A
    {
    public:
    
    	A()
    	{
    		cout << "In constructor A" << endl;
    	}
    };
    
    class B
    {
    public:
    
    	A a;
    
    	B()
    	{
    		cout << "In constructor B" << endl;
    	}
    };
    
    int main()
    {
    	B b;
    
    	return 0;
    }

    thanks in advance,
    George

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, all member variables that are of a class type are implicitly called [if not explicitly called] when the instance is constructed. This is part of the basics of object oriented programming - since you can't actually know (necessarily) the base class of your current object if it's inherited from another class, so you you wouldn't know the chain of constructors that you would need to call - in particular since the first call has to be to the most basic constructor. Say for example you have a "ifstream" in your class, you wouldn't know what the base-class(es) is(are) for that class, right? But the compiler can figure it out and call the constructor.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Code:
    	B()
    	{
    		cout << "In constructor B" << endl;
    	}
    is equivalent to:
    Code:
    	B() : a()
    	{
    		cout << "In constructor B" << endl;
    	}
    The compiler automatically calls the default constructor of a member object if you don't explicitly initialize it in the initializer list.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Mats,


    My question is answered.

    Quote Originally Posted by matsp View Post
    Yes, all member variables that are of a class type are implicitly called [if not explicitly called] when the instance is constructed. This is part of the basics of object oriented programming - since you can't actually know (necessarily) the base class of your current object if it's inherited from another class, so you you wouldn't know the chain of constructors that you would need to call - in particular since the first call has to be to the most basic constructor. Say for example you have a "ifstream" in your class, you wouldn't know what the base-class(es) is(are) for that class, right? But the compiler can figure it out and call the constructor.

    --
    Mats

    regards,
    George

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Daved,


    My question is answered.

    Quote Originally Posted by Daved View Post
    Code:
    	B()
    	{
    		cout << "In constructor B" << endl;
    	}
    is equivalent to:
    Code:
    	B() : a()
    	{
    		cout << "In constructor B" << endl;
    	}
    The compiler automatically calls the default constructor of a member object if you don't explicitly initialize it in the initializer list.

    regards,
    George

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Also, in anticipation of your next question, member variables are always initialized in the order that they are declared in the class declaration, not in the order they are specified in member initialization lists. For example:
    Code:
    class Test
    {
    public:
        Test() : m_Str(), m_List(), m_Vec() {}
    
    private:
        std::list<double>  m_List;
        std::string        m_Str;
        std::vector<int>   m_Str;
    };
    In this example, m_List will be initialized before m_Str, even though the constructor makes it look like m_Str is being initialized first.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    And a proper compiler will warn you about it.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array initialization with int variable
    By tsantana in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2009, 02:48 PM
  2. Replies: 6
    Last Post: 07-29-2008, 04:37 AM
  3. Replies: 4
    Last Post: 11-30-2005, 02:05 PM
  4. variable initialization error...
    By sunoflight77 in forum C++ Programming
    Replies: 2
    Last Post: 04-06-2005, 12:01 AM
  5. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 11:14 AM