Question:
what is a copy constructor ?
Aman
2012-05-16 19:51:26 UTC
i just want an overview and how it is significant ?
Four answers:
Platinum
2012-05-16 19:56:38 UTC
A copy constructor is a special constructor in the C++ programming language for creating a new object as a copy of an existing object. The first argument of such a constructor is a reference to an object of the same type as is being constructed (const or non-const), which might be followed by parameters of any type (all having default values).



Normally the compiler automatically creates a copy constructor for each class (known as a default copy constructor) but for special cases the programmer creates the copy constructor, known as a user-defined copy constructor. In such cases, the compiler does not create one. Hence, there is always one copy constructor that is either defined by the user or by the system.



A user-defined copy constructor is generally needed when an object owns pointers or non-shareable references, such as to a file, in which case a destructor and an assignment operator should also be written .



Copying of objects is achieved by the use of a copy constructor and an assignment operator. A copy constructor has as its first parameter a (possibly const or volatile) reference to its own class type. It can have more arguments, but the rest must have default values associated with them.
2012-05-16 21:26:50 UTC
The copy constructor lets you create a new object from an existing one by initialization. A copy constructor of a class A is a non-template constructor in which the first parameter is of type A&, const A&, volatile A&, or const volatile A&, and the rest of its parameters (if there are any) have default values.



If you do not declare a copy constructor for a class A, the compiler will implicitly declare one for you, which will be an inline public member.



The following example demonstrates implicitly defined and user-defined copy constructors:



#include

using namespace std;



struct A {

int i;

A() : i(10) { }

};



struct B {

int j;

B() : j(20) {

cout << "Constructor B(), j = " << j << endl;

}



B(B& arg) : j(arg.j) {

cout << "Copy constructor B(B&), j = " << j << endl;

}



B(const B&, int val = 30) : j(val) {

cout << "Copy constructor B(const B&, int), j = " << j << endl;

}

};



struct C {

C() { }

C(C&) { }

};



int main() {

A a;

A a1(a);

B b;

const B b_const;

B b1(b);

B b2(b_const);

const C c_const;

// C c1(c_const);

}



The following is the output of the above example:



Constructor B(), j = 20

Constructor B(), j = 20

Copy constructor B(B&), j = 20

Copy constructor B(const B&, int), j = 30
James Bond
2012-05-16 21:53:39 UTC
What all other responders gave is correct. In addition, in practice we have to know about shallow copying and deep copying. In shallow copying member by member assignment takes place which leads to memory leaks, and other related problems.

Consider

class xyz

{

int *a,sz;

};



It i create a copy constructor( or use default copy constructor) then a and sz values are copied. Thus, both the objects will be pointing same memory through their a values. If one object's scope is over then the object a's will be pointing a dangling memory, a serious mistake.



To safeguard against this, while copying you create a new array of size sz and then copy the contents pointed source objects a to destination object which is called deep copying.
?
2012-05-16 22:31:25 UTC
A copy constructer is a special type of constructer which is used to initialize one object with the content of another object



The copy constructer can be



(1)System defind

(2)user defined





When we are having a Class A having no pointer members then we may not go for user defined and the system defined copy constructer can do memberwise initialization



example:



class with no pointer datamembers



class A

{

public:

A()

{

x=0;

y=0;

}

private:

int x;

int y;



};



int main()

{

A ob1;

A ob2(ob1);//system defined copy constructer

}





note:

we can also write system defined copy constructer here also.





class with pointer member



CODE:(user defined copy constructer )







class A

{

public:

A()

{

x=0;

y=new int(0);

}

A(const A &ob1)

{

x=ob1.x;

y=ob1->y;

}

private:

int x;

int *y;



};



int main()

{

A ob1;

A ob2(ob1);//user defined copy constructer

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...