1

In Below code d = a; doesn't calls copy constructor.

How a is copied to d ? or In what cases we have to Overload the = operator ?

#include<iostream>
using namespace std;

class code{    
    int id;

    public:    
    code(){}//default constructor

    code(int a){
            id=a;
    }

    code(code & x){//copy constructor
        id=x.id;
    }

    void display(){
        cout<<id;
    }
};

int main(){


    code a(100);
    code b(a);//copy constructor is called
    code c=a;//copy constructor is called again
    code d;
    c=a;//copy constructor is not called this time

    std::cout << "\n id of A: ";
    a.display();
    cout << "\n id of B: ";
    b.display();
    cout << "\n id of C: ";
    c.display();
    cout << "\n id of D: ";
    d.display();
    return 0;
}

how can I effectively implement copy constructor ?

Rajat
  • 2,467
  • 2
  • 29
  • 38

4 Answers4

1

According to the C++ Standard (12.8 Copying and moving class objects)

18 If the class definition does not explicitly declare a copy assignment operator, one is declared implicitly. If the class definition declares a move constructor or move assignment operator, the implicitly declared copy assignment operator is defined as deleted; otherwise, it is defined as defaulted (8.4). The latter case is deprecated if the class has a user-declared copy constructor or a user-declared destructor. The implicitlydeclared copy assignment operator for a class X will have the form

28 The implicitly-defined copy/move assignment operator for a non-union class X performs memberwise copy-/move assignment of its subobjects.

In this statement

c=a;

there is used the implicitly defined by the compiler copy assignment operator because the class did not define it explicitly and it performs member-wise copy of object data members (for this class definition it is data member int id;)

You need explicitly to define the copy assignment operator when member-wise copying of subobjects of the object does not satisfy class requirements.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

The copy constructor is not called because the implicitly-defined copy-assignment operator is called instead. It is of the form:

T& T::operator=(const T&)
TartanLlama
  • 63,752
  • 13
  • 157
  • 193
0

Copy constructors can be called during initialization:

code d = a;

In your code:

code d;
d = a;

The operator= is getting called.

VP.
  • 15,509
  • 17
  • 91
  • 161
0

How The a is copied to d ?

Using the copy-assignment operator. The copy constructor is for initialisation, not assignment.

In what cases we have to Overload the = operator ?

When you need something other than the default behaviour of recursively copying each sub-object. In your case, the default behaviour is to simply copy the id value. That's also the default behaviour of the copy-constructor, so you don't need to provide that either.

The Rule of Three is a good guideline for when you need to provide these operators. You typically need them when your class is managing the lifetime of another resource, which it releases in its destructor. You need non-standard copy semantics so that you don't get two objects trying to release the same resource.

Community
  • 1
  • 1
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644