copy constructor

K

Kittu

Why should we use the copy constructor.Which is the best way to
initialize object using copy constructor or using assignment operator
and why?
 
V

Victor Bazarov

Kittu said:
Why should we use the copy constructor.Which is the best way to
initialize object using copy constructor or using assignment operator
and why?

We should _implement_ a copy constructor if we care how the object is
constructed from another object of the same type. The reason (for the
'why' question) is that the language rules dictate that copies are made
in certain situations, and if you don't provide the copy c-tor, the
compiler will attempt to create one for you. The same with the copy
assignment operator.

The most common situation in which a copy c-tor and a copy assignment
operator is needed is described in "The Rule Of Three". Look it up.

Victor
 
R

Ron Natalie

Kittu said:
Why should we use the copy constructor.Which is the best way to
initialize object using copy constructor or using assignment operator
and why?
You can't initialize things with an assignement operator.

Frankly, if you need an assignment operator, you most likely
also need a copy constructor (and a destructor as well) as
the compiler and other standard libraries will tend to use
them both to copy objects depending on context.
 
R

Rolf Magnus

Kittu said:
Why should we use the copy constructor.

Uhm, for copying objects?
Which is the best way to initialize object using copy constructor or using
assignment operator and why?

Only constructors can initialize the object, so it's not a question of a
"best" way.
 
P

Peter Koch Larsen

Rolf Magnus said:
Uhm, for copying objects?


Only constructors can initialize the object, so it's not a question of a
"best" way.
I'll tell you for USD 50. Ok - i'll accept USD 25 if you give me credit when
you return your assignment.

/Peter
 
P

Peter Koch Larsen

Rolf Magnus said:
Uhm, for copying objects?


Only constructors can initialize the object, so it's not a question of a
"best" way.
Hi Rolf


Sorry to post in a wrong location - the reply was meant for Kittu, of
course. Hope he sees it anyway ;-)

/Peter
 

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

No members online now.

Forum statistics

Threads
474,181
Messages
2,570,970
Members
47,537
Latest member
BellCorone

Latest Threads

Top