How constructors are called in c++ (inheritance)

This is a discussion on How constructors are called in c++ (inheritance) within the C++ Programming forums, part of the General Programming Boards category; struggling with constructors in c++.. why in a code below base class default constructor is called despite of constructor with ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    2

    Question How constructors are called in c++ (inheritance)

    struggling with constructors in c++.. why in a code below base class default constructor is called despite of constructor with parameter being already executed? In D class constructor with parameter D:(int n) : B(n) constructor is called explicitly first but after that default constructor B() is still executed before anything else and I couldnt figure out why? Thank you for any help.

    Code:
    class B
       { 
          public:
             B();
             B(int n);
       };
    
       B::B()
       {
          cout << "B::B()\n";
       }
    
       B::B(int n)
       {
          cout << "B::B(" << n << ")\n";
       }
    
    
       class D : public B
       { 
          public:
             D();
             D(int n);
          private:
             B b;
       };
    
       D::D()
       {
          cout << "D::D()\n";
       }
    
       D::D(int n) : B(n)
       {
          b = B(-n);
          cout << "D::D(" << n << ")\n";
       }
    
       int main()
       {
          D d(3);
          return 0;
       }
    output is:

    B::B(3)
    B::B()
    B::B(-3)
    D:(3)

    sorry about smiley above, should be :: there

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> but after that default constructor B() is still executed before anything else and I couldnt figure out why?

    You have a member variable of type B inside class D. That will be constructed when a variable of type D is created. That construction happens in the initialization list. If you don't explicitly construct b inside the initialization list, the default constructor will be called before the constructor's code is executed. That means that this line:
    Code:
    b = B(-n);
    comes after b was already default constructed. It then creates a temporary B object with -n and assigns that temporary to b.

    If you created an assignment operator and added a cout output to it, you would see that that code is assigning to b, not constructing it.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    It's called because you don't initialize b to anything. Therefore, it gets initialized to the default value: a default constructor.

    To see the output you were expecting, use this:
    Code:
       D::D(int n) : B(n), b(-n)
       {
          //b = B(-n);
          cout << "D::D(" << n << ")\n";
       }
    See, there's a difference between assignment and initialization.

    BTW, you can turn off smileys for the entire post, or put code in noparse tags like so: [b][/b][noparse]:)[/noparse]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    2
    Thank you for such a quick help. I cant believe I missed private b variable, now it makes perfect sense to me. Thank you again for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. inheritance and constructors
    By coletek in forum C++ Programming
    Replies: 6
    Last Post: 07-04-2009, 01:35 AM
  2. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  3. Inheritance and Polymorphism
    By bench386 in forum C++ Programming
    Replies: 2
    Last Post: 03-18-2004, 09:19 PM
  4. Inheritance vs Composition
    By Panopticon in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2003, 03:41 AM
  5. help with class and constructors
    By stautze in forum C++ Programming
    Replies: 14
    Last Post: 10-16-2002, 07:56 PM

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