0

I understand that the copy constructor is invoked when a new object based on an existing object is created. However I have been trying to do the same, and I find that the copy constructor is not being invoked. Following is my piece of code:

/*
* class definition
*/
class A
{

  public:
     A(int mn, int mx); //default constructor
     A(A const& obj);  //copy constructor
     ~A();
  private:
    int* ptr;
    int max;
    int min;
}

class B
{
  public: 
      B();
      void print();
  private: 
      A m_aObject;
}


/*
* B.cpp
*/
void B::print()
{
    A local_obj1(2,3);
    local_obj1.ptr = Global_array; //some global array.
    m_aObject = local_obj1; // should invoke the copy constructor
}


/*
* A.cpp
*/
A::A(A const& obj)
{
   cout << "copy constr. invoked"<<endl;
   ptr = new int[10];
   for(int i= 0; i< 10; i++)
     ptr[i] = obj.ptr[i];       
}

A::A(int mx, int mn)
{
  min = mn;
  max = mx;
}

As per the link [https://www.tutorialspoint.com/cplusplus/cpp_copy_constructor.htm], the line m_aObject = local_obj1; must invoke the copy constructor.

However I see that after the execution of the above statemment, the copy constructor is never invoked. The print inside the copy constr. is never displayed on the console.

Does the line m_aObject = local_obj1; really invoke the copy constr. ?

If I try invoking the copy constr. by m_aObject(local_obj1);, it gives a compilation error error: no match for call to '(A) (A&)'

Is there any other way of invoking the copy constructor for the above case.

gst
  • 1,251
  • 1
  • 14
  • 32

3 Answers3

3

Two things can happen when using = with classes. The first is copy construction and that looks like

type name = some_value;

The other is copy assignment and that happens when you have

name = some_value;

As you can see your code looks like the second example which means you are using the assignment operator and not the copy constructor. Remember that constructors are only ever called when an object is created. After that you are only copy/move assigning.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
1

m_aObject = local_obj1; // should invoke the copy constructor

No. It shouldn't.

It invokes the copy assignment operator. You don't have a custom implementation. Hence, the compiler generates one for you.

See What's the difference between assignment operator and copy constructor?.

Community
  • 1
  • 1
R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

In your class B you have the class A as a member which is initialized during the construction of class B. Since the member m_aObject has already been initialized (created using the default constructor of A) the copy constructor will not be called. In your case the default assignment operator is being called using a shallow copy.

For reference the copy constructor is generally called when:

  • instantiating one object and initializing it with values from another object.
  • passing an object by value
  • an object is returned from a function by value
Carlos
  • 358
  • 2
  • 10