copy constructor

K

Kittu

In which situations, we need to overload copy constructors? What is
exactly practical and theoritically difference between using copy
constructor and assignment operator? Please give the complete answer
in detail as I am not getting satisfactory answer regarding this.
 
K

Karl Heinz Buchegger

Kittu said:
In which situations, we need to overload copy constructors? What is
exactly practical and theoritically difference between using copy
constructor and assignment operator? Please give the complete answer
in detail as I am not getting satisfactory answer regarding this.

It is extremly simple:

copy constructor
As all constructors, it is used when a *new* object is created.
But this time, the new object should be a copy of an already existing
object (it should be initialized with that object).
Hence the name: *copy* constructor

assignment operator
Is used, whenever an already existing object is assigned the value
of another object.

When you don't write any of them, then the compiler generates one for you.
The generated ones do: memberwise initialization resp. memberwise assignment
You have to write each of those functions by yourself, if this default
behaviour is not sufficient. Any book on C++ will enlighten you, when this
case arises. Alternatively you can search the web for: 'Rule of three' which
is somewhat related to that and its explanation usually also contains an
explanation of when you need to write those functions by yourself.
 
Joined
Apr 1, 2009
Messages
1
Reaction score
0
i didnt get

Karl Heinz Buchegger,


please help me out....regarding the issue of difference between Copy constructor and Assignment operator....
please explain both with example independently and tell the difference between dem..

please .....please...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,426
Latest member
MrMet

Latest Threads

Top