Question on Copy constructor!!

P

prashna

Hi all,
Why is it necessary to declare a parameter to a copy constructor as a
reference?Thanks in Advance
 
R

Rolf Magnus

prashna said:
Hi all,
Why is it necessary to declare a parameter to a copy constructor as a
reference?Thanks in Advance

Think of what happens if it's not a reference. The value gets passed as
a copy. But to get passed as a copy, the copy constructor would be
called. That copy constructor gets its value by copy, so the copy
constructor must be called, for which the copy constructor would need
to be called, which needs.... you get the point. It produces an
infinite recursion of copy constructor calls.
 
J

Jerry Coffin

Hi all,
Why is it necessary to declare a parameter to a copy constructor as a
reference?Thanks in Advance

Because taking its argument by value would cause infinite recursion
(when you pass something by value, you're passing a copy of the
original item...)

This basic question has plagued computer science for decades. I'm
pretty sure it was at least 30 years ago that this question came up
wrt Lisp, prompting a paper named something like "Should cons cons its
inputs?". The answer was basically the same, but back then it took a
whole paper to say "pass by reference must be used to avoid infinite
recursion." :)
 
A

Ajay Baranwal

Rolf Magnus said:
Think of what happens if it's not a reference. The value gets passed as
a copy. But to get passed as a copy, the copy constructor would be
called. That copy constructor gets its value by copy, so the copy
constructor must be called, for which the copy constructor would need
to be called, which needs.... you get the point. It produces an
infinite recursion of copy constructor calls.

Further to add, why can't we have the parameter to be a pointer ? I
believe then we need to copy each of the data members in the object
pointed by the pointer. And that's safe. No infinite recursion. But it
could be a dangling or uninitialized pointer coming as parameter which
would give core dump when try to access the real object!

Thanks!
 
K

Karthik

prashna said:
Hi all,
Why is it necessary to declare a parameter to a copy constructor as a
reference?Thanks in Advance

Else, it does not make sense for the code generator.

Consider a function like this -

1- void fun ( X fun )


2- void fun ( X & fun)

What essentially happens is that, in case 1, a copy of object X is
created (copy constructor is invoked, if X is a user-defined data type)
pushed onto the function stack, and operated on.

In case 2, a reference of the object instead of the object is pushed
onto the stack.


Coming back to copy constructor -


Assume you have X ( X ) as the signature of copy constructor, you would
eseentially get into an infinite loop generating the code ( or in other
words, it does not make sense to do so).
But X ( X & ) is fine.

This is explained in detail in the book - 'C++ programming language'
written by the author of C++, Stroustroup.. Grab one if you are into C++.
 

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,169
Messages
2,570,919
Members
47,459
Latest member
Vida00R129

Latest Threads

Top