0

As i know, when we assign one object is another default copy constructor will be called.

class class1 obj1;
class class2 obj2;
obj1(obj2); //default copy constructor will be called by compiler 

So, when should I write explicitly the copy constructor?

bapi
  • 1,747
  • 9
  • 30
  • 53
  • 3
    In your example, it is the *assignment operator* that gets called, not the copy constructor. This is unrelated to when you should explicitly write a copy constructor. Edit: now it isn't calling either the assignment operator or the copy constructor, but some overload of `operator()`. – juanchopanza Mar 29 '14 at 08:23
  • 1
    Are you asking when to code up the operator, or when to use which syntax? – doctorlove Mar 29 '14 at 08:27

4 Answers4

8

In your case the copy-assignment operator will be called, not the copy-constructor. To call the copy-constructor you would have do to e.g.

class1 obj1;
class1 obj2 = obj1;  // Invokes the copy-constructor in obj2

A good idea when to write a copy-constructor (or a copy-assignment operator, or a destructor) you can see by reading about the rule of three. In short, if you have any of a destructor, copy-constructor or copy-assignment operator, then you should probably have all of them.


Also, while the compiler will auto-generate copy-constructor and copy-assignment operator for you if you do not provide your own, you have to remember that those auto-generated function will only do a shallow copy. If you have e.g. pointers to memory you allocate in the object, the auto-generated functions will only copy the actual pointer, and not what it points to. This means that after a copy you have two objects both pointing to the same memory. If you delete the pointer in the destructor, and one of the objects are destructed, the other object will still have its pointer, but it will now point to deleted memory.

Some programmer dude
  • 363,249
  • 31
  • 351
  • 550
0

You will get a default copy constructor when you don't write one, provided you don't add any of the three or five to your class : destructor, copy or move assignment or constructor.

Sometimes this does the right thing. For example, if you just need a shallow copy, or if the member's corresponding functions fo the right thing, for example smart pointers.

Community
  • 1
  • 1
doctorlove
  • 17,477
  • 2
  • 41
  • 57
-1

If an object exist prior to the assign, then it does not involve a construction but the assignment operator, signatures are:

T& operator=( T const & ); // from l-value ref
T& operator=( T && ); // from r-value ref, since c++11

A frequent strategy is to write the assignment operator as the idiom "copy and swap" :

T& operator=( T const & o ) {
    T val( o );    // you need to write the copy ctor
    swap(*this,o); // you need to write the swap
    return *this;
}
T& operator=( T && o ) {
    T val( std::move(o) );    // you need to write the move ctor
    swap(*this,o); // you need to write the swap
    return *this;
}

The c++11 version of that strategy

T& operator=( T o ) noexcept { // copy done before the operator that can be noexcept ( swap have to too)
    swap(*this,o); // you need to write the swap
    return *this;
}
galop1n
  • 8,042
  • 17
  • 34
-1

In some cases you will find that the way your objects should be copied is not trivial. If you consider the class :

class Car {
 string BrandName;
 int NumberOfPassenger;
}

Then it is clear that when you'll be copying two objects, you'll simply want to copy them member by member. There's nothing special to do here so the defaut copy constructor will work just fine.

But imagine that the class is instead :

class Car {
 string BrandName;
 int NumberOfPassenger;
 Mechanics EngineeringStuff;
}

Here Mechanics is a reference type. What the copy constructor will do is simply copying the reference to the new object, so both cars - car1 and car2 - will share the same EngineeringStuff. But a more natural behaviour would be to allocate manually a new Mechanics object when performing the copy, so the cars don't share the same wheels, motors etc...

More generally, it's usually when you have to deal with reference types or certain kind of business logic that you will need to explicitly write your copy constructor.

tobiak777
  • 2,701
  • 1
  • 24
  • 40