Default Copy Constructor

Why is it recommended to provide implementation of copy constructor instead of using compiler provided " default copy constructor" ?

This is in reference to create new instance of Class from existing one.

Foo object2 = object1 ;


The default copy constructor only copies the values of the class members. This is not a problem for classes such as this:
1
2
3
class A{
    int a;
};

But for classes which use pointers, it sometimes is desirable to also copy the objects those pointers point to. For example:
1
2
3
4
5
6
7
8
9
class B{
    A *a;
	B(){
		this->a=0;
	}
	B(const B &copy){
		*(this->a)=*(copy.a);
	}
};
thanks for reply , really appreciates ..
If anyone has more insight or links please post them as well.
Because .....


e.g

if we dont provide copy constructor the compiler automatically made a copy constructor but it will do SHALLOW COPY , if you are using pointers than it will point to the same place , both the original one and the new one ...

Copy constructor helps you too handle it seperately ....
Insted of this we need to do DEEP copy use GOOGLE to see the difference
Topic archived. No new replies allowed.