Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Base Copy constructor not called

class Base
{
      public:
      int i;

      Base()
      {
          cout<<"Base Constructor"<<endl;
      }

      Base (Base& b)
      {
          cout<<"Base Copy Constructor"<<endl; 
          i = b.i; 
      }


      ~Base()
      {
          cout<<"Base Destructor"<<endl;
      }

      void val()
      {
             cout<<"i: "<< i<<endl;
      }      
};

class Derived: public Base
{
      public:
      int i;

      Derived()
      {
          Base::i = 5;     
          cout<<"Derived Constructor"<<endl;
      }

      /*Derived (Derived& d)
      {
          cout<<"Derived copy Constructor"<<endl;
          i = d.i; 
      }*/

      ~Derived()
      {
          cout<<"Derived Destructor"<<endl;
      }      

      void val()
      {
             cout<<"i: "<< i<<endl;
             Base::val();
      }
};

If i do Derived d1; Derived d2 = d1; The copy constructor of base is called and default copy constructor of derived is called.

But if i remove the comments from derived's copy constructor the base copy constructor is not called. Is there any specific reason for this? Thanks in advance.

like image 701
nitin soman Avatar asked Oct 01 '09 04:10

nitin soman


People also ask

Why is my copy constructor not being called?

The reason the copy constructor is not called is because the copy constructor itself is a function with one parameter. You didn't call such function,so it didn't execute.

Does default copy constructor call base copy constructor?

If the user defines its own copy constructor in B , when invoked, this copy constructor will call the base class default constructor, unless a call to the base class copy constructor is explicitly present (e.g. in the initialization list).

Under which conditions a copy constructor is called?

A copy constructor is a member function that initializes an object using another object of the same class. The Copy constructor is called mainly when a new object is created from an existing object, as a copy of the existing object.


2 Answers

I think you have to explicitly call the base copy constructor:

  Derived (Derived& d) : Base(d)
  {
      cout<<"Derived copy Constructor"<<endl;
      i = d.i; 
  }
like image 69
tster Avatar answered Oct 18 '22 14:10

tster


If you want to read actual rule you should refer to C++ Standard 12.8/8:

The implicitly-defined copy constructor for class X performs a memberwise copy of its subobjects. The order of copying is the same as the order of initialization of bases and members in a user-defined construc- tor (see 12.6.2). Each subobject is copied in the manner appropriate to its type:

  • if the subobject is of class type, the copy constructor for the class is used;
  • if the subobject is an array, each element is copied, in the manner appropriate to the element type;
  • if the subobject is of scalar type, the built-in assignment operator is used.

When you define copy constructor explicitly you should call copy c-tor of base class explicitly.

like image 34
Kirill V. Lyadvinsky Avatar answered Oct 18 '22 14:10

Kirill V. Lyadvinsky