473,394 Members | 1,935 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Question on Copy constructor!!

Hi all,
Why is it necessary to declare a parameter to a copy constructor as a
reference?Thanks in Advance
Jul 22 '05 #1
4 1876
prashna wrote:
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.

Jul 22 '05 #2
va******@rediffmail.com (prashna) wrote in message news:<d4**************************@posting.google. com>...
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." :-)
--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #3
Rolf Magnus <ra******@t-online.de> wrote in message news:<c7*************@news.t-online.com>...
prashna wrote:
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.


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!
Jul 22 '05 #4
prashna wrote:
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++.
--
Karthik.
Humans please 'removeme_' for my real email.
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Kench | last post by:
Sorry if this becomes a repost. I posted this to comp.lang.c++.moderated 1 hour ago still it does not show up there so posting this here. Hi, Consider class A & B both of which implement a copy...
11
by: Kurt Krueckeberg | last post by:
Given a class X class X { public: X(int); X(const X&); //. . . }; Is this line X x1(1); the same thing as X x2 = 2;
8
by: trying_to_learn | last post by:
Why do we need to explicitly call the copy constructor and the operator = , for base class and member objects in composition? ....book says "You must explicitly call the GameBoard copy-constructor...
8
by: Jesper | last post by:
Hi, Does the concept "copy constructor" from c++ excist in c#. What is the syntax. best regards Jesper.
14
by: Arne | last post by:
In C++ we have a copy constructor. What is the equivalent in .Net? Would that be a clone method?
19
by: Jeroen | last post by:
Hi guys, I have a simple question. If I have a class like: class A { A(); ~A(); A(A& a); A(int i);
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
4
by: Rahul | last post by:
Hi Everyone, It is well known that the input parameter which is passed to the copy constructor is passed as reference and not as as object. Because passing an object is as good as making another...
1
by: Visame | last post by:
#include <iostream> using namespace std; class A { public: A() {cout << "constructor A" << endl;} A(A& a) {cout<<"copy A"<<endl;} virtual void Test(){cout <<"A test"...
6
by: suresh | last post by:
Hi Could you please tell why copy constructor is not called in the first line in main(), as mentioned in the text books. I used g++ version 4.1.2 on debian etch thanks suresh # include...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.