The rule is that if you need to provide either:
- copy constructor or
- destructor or
- copy assignment operator
then you probably need to provide all three of them. This rule is known as Rule of Three.
Is it safe not to declare a copy constructor?
It is safe.
Do you have to for your example case?
Not really. To be specific the rule of three governs that. Check the linked question for more details on that.
How does the default copy constructor looks like?
I gather this is asking, What does the default copy constructor do.
This is answered in:
C++03 Standard 12.8 Copying class objects:
Para 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 constructor (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.
Virtual base class subobjects shall be copied only once by the implicitly-defined copy constructor (see 12.6.2).
Even If I not declare a copy constructor, the default one will be called when I call operator=()
A copy constructor is invoked only when a copy of the class object needs to be created. This involves copies of objects created while passing to or returning from functions.
Your copy assignment operator passes the object A
by value, this pass by value is achieved by passing a copy of the object through copy constructor and hence the call to copy constructor.
To avoid the copy you need to pass by reference:
A& A::operator=(const A& other)
Good Read:
What's the difference between passing by reference vs. passing by value?