Hello to all, can anyone please explain to me the following: I know how to use the copy constructor, I know that it's used only in the initialization, when i wanna initialize a new object using an old object... But what I don't understand is how will I know when I have the need to do this - "initialize a new object using an old object" ? How will i recognize the need of using the copy constructor ?

Recommended Answers

All 5 Replies

You're asking the wrong question. The compiler will attempt to invoke a copy constructor (assuming one exists) upon any attempt to create a copy of an object.

The real practical concern is whether you need to write the copy constructor yourself or let the compiler do it for you.

If you don't supply a copy constructor, the compiler will generate one by default. The compiler generated default invokes copy constructors of any base classes (in defined order) and then does a memberwise copy of any members of the class.

The general rule is that, if the compiler-generated default differs from the behaviour you need (eg managing a global resource) then you need to roll your own copy constructor.

The most common case is when a class contains a pointer, and the class uses dynamic memory allocation. The default-generated copy constructor copies the value of the pointer, so that two objects end up containing a pointer to the same thing. That makes things problematical for the destructor: when both objects are destroyed, the pointer is released twice, causing undefined behaviour. So you need to implement a copy constructor in this case to do a "deep copy".

One other rule of thumb is that any class that requires the programmer to write a copy constructor also requires the programmer to write an assignment operator.

Small addition.
Question: how will I know when I have the need to do this:

double pi = 3.14159265358;

Answer: YOU know when and why...
It's the same case.

Also you are using copy constructor implicitly when you pass an object as a function argument by value (it's not an assignment).

Hello to everyone!!!!!
i m new to c++ and i want to know the use of copy and overloading constructor.How can i ADD TWO COMPLEX NO. USING COPY CONSTRUCTOR?
thanks in advance for help!!!!!!!!

Hello to all, can anyone please explain to me the following: I know how to use the copy constructor, I know that it's used only in the initialization, when i wanna initialize a new object using an old object... But what I don't understand is how will I know when I have the need to do this - "initialize a new object using an old object" ? How will i recognize the need of using the copy constructor ?

hello i am a student i have learned c++ as i am concerned about copy contructor that copy constructor is a constructor which uses the reference to its own class as a parameter...
it can be declared as follows:-
suppose classname is time,
time t1 ( t2 );
if u r not satisfied reply ur question on skhandre02@gmail.com

>> How will i recognize the need of using the copy constructor ?

Usually when you have a class that uses dynamic memory then you need
to create your own copy constructor so when copying a value, each
variable has its own memory location for its member variables.

For example :

//say myString is a class that is similar to a string class which 
//uses new to dynamically allocated a char array
myString str("hello");
myString str2(str); //what happens here ?

In the second line str2 invokes the copy constructor.

Now imagine 2 cases :

case 1 : There is no copy constructor defined.
case 2 : You have created a proper copy constructor

Now for case 1 : what would happen is that str2 and str1 will both
hold a pointer to the same object that str1 uses inside its class.
So when str1 goes out of scope, str2 points to a dud memory. This
is a huge bug.

Now for case 2: str2 invokes its copy ctor and makes a deep copy of
str1. So that str2 has the same value as str1 but has its own memory space for each of its variables.

So in general create a copy constructor , regular constructor, destructor and assignment operator, when your class dynamically allocates
memory for a variable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.