Link to home
Start Free TrialLog in
Avatar of TCSD
TCSD

asked on

Copy constructor

Dear all,
Kindly request somebody who could give a clear idea of Copy constructor and when they are used?
Early reply appreciated.Thanks in advance.
Avatar of DarthNemesis
DarthNemesis

A copy constructor is used by a class when you create a new class object using an existing class object. For instance:

#include <string>
using namespace std;

void main () {
        string s = "12345";
        string t(s); // this uses the copy constructor for the string class
}
A copy constructor is (of course) a special kind of constructor for a class. It takes the form of:

class AClass{
public:
// Copy constructor
AClass(const AClass& rhs);
}

The role of the copy constructor is to provide the compiler a means of creating instances of class A from other instances of class A. Copy constructors are called more often than you think:

1. When you explicitly call them to initialize a new instance of a class:
AClass a; // Default constructor
AClass b(a); // Copy constructor
AClass b = a; // ALSO a copy constructor call

2. When you pass your class by value as a parameter for a function:
void f(AClass p);

int main() {
   AClass a;
   f(a);
   return 0;
}

In this case, the compiler will look at the parameter you pass the function, and then make a copy of it using the constructor for the function to use.

3. Again, when you return a class:
This is similar to case 2 above. When you return an instance of your class, the compiler calls the copy constructor. Neither case 2 or 3 apply when passing by -reference- (because you're not making copies there).

As a general rule, if there's a need for a temporary instance of your class to be created, the compiler will probably end up calling the copy constructor to do it.
The best way to learn as much about constructers as possible is to look at some examples. As previous posters have stated, a copy constructer is just a special kind of constructer. The best way to look at constructers is to look at a source file. Wherever your compiler is installed, there should be a folder called "include". Open that, and there's all of the #include<somfile.h> files. Just find one that says vector or matrix or string (any random one will problably do, as long as there's a class in it) and look it over. That's how I learned all about classes.

note: sometimes there is a .cpp file that the actual implementation is in, you should read that. The .h file just gives you the prototypes for the member functions. You gotta read the .cpp file
Oh, I forgot my punchline. :)

BECAUSE the copy constructor for your class is called WHENEVER you pass by value and not by reference, you should try to avoid passing by value whenever you can.

Say you have the class I semi-wrote above, and you have a function that does something really simple... like returns what you give it.

AClass giveitback(AClass input)
{ return input; }

In this case, you have the copy constructor called both when you send "input" to your function and again when you return "input" back. You can imagine for more complicated classes, this sort of overhead is really undesirable.

You'd do much better (when you can) to do it this way:

AClass& giveitback(AClass& input)
{ return input; }

And never call the copy constructor.
Avatar of TCSD

ASKER

Thanks  .But how does it act when deleting the object.
AClass a
AClass b= a
But when U delete a , will b also be deleted...
Just for my understanding...
No, b will not be deleted. When the copy constructor is called in your declaration for B, it makes a copy of A in a new memory location. Deleting the original object A from memory won't affect the new object B.
As Kashra pointed out, passing an object by reference saves memory because it doesn't invoke the copy constructor and thus doesn't use any more memory. It's in _this_ case, when you create a reference or a pointer to an object, that deleting the original object will invalidate the reference or pointer, because they all point to the same memory location.
One of the most important reasons for creating your own copy constructor (as opposed to the one automatically created by the compiler) is to perform what is known as a deep copy.

If a class contains pointer members, the default copy constructor just does a member-by-member copy of the data. That means you now have two objects pointing to the same data. This can be bad news if the destructor frees the pointer because now you will have two objects trying to free the same pointer, or one object pointing to bad data.

There are other reasons too. For instance, maybe two objects can't have the same value for a member called 'm_Name'. In your copy constructor, you could append "Copy_Of_" to the name being copied.
Avatar of TCSD

ASKER


Thanks everybody!
Keitha1,
This was the one I was confused.So could U explain detailed with example.Class having pointer member and same value for a member..
ASKER CERTIFIED SOLUTION
Avatar of keitha1
keitha1

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of TCSD

ASKER

Thanks for the wonderful explanation and sorry for troubling U!
Well Good Night!
Avatar of TCSD

ASKER

Thanks for the wonderful explanation and sorry for troubling U!
Well Good Night!