Click here to Skip to main content
15,846,571 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
what happen when we use copy contructor by call by value method?
if any error why this error is occured?

To pass an object by value to a function, then that object needs to be copied to the parameter object.

So the copy constructor would be called...

...which is not yet defined!

As the compiler does not have psychic powers to solve circular dependencies, you get an error.

If you want to make your code safe, then use a const reference as your parameter.

Share this answer
patilvaibhavrao 22-Mar-11 6:58am    
whar are the circular dependies? please explain
Iain Clarke, Warrior Programmer 22-Mar-11 7:20am    
Iain Clarke, Warrior Programmer - 19 mins ago
Have a look here:
In short.
A depends on B.
B depends on C.
C depends on A.

I hope that helped,

patilvaibhavrao 23-Mar-11 5:16am    
why compiler should not go in infinite when we r using call by reference in copy constructor
Iain Clarke, Warrior Programmer 23-Mar-11 7:29am    
This is going to be a little hard to understand, but when you do, you'll go "ahah!!".

When you pass an object by value, the function gets a whole new object to play with - it's copied from the "original".

If you pass a pointer to an object, then the function gets a pointer to the original. If you make changes to the object in the function, then the original gets changed. Try experiment with this - make a function, taking a pointer to something simple, like a CPoint.

When you define a pointer to an object, you don't have to define the object. A pointer to anything is the same size. [99% true]. A pointer is just a memory location value. The compiler is nice, and makes sure you don't make easy mistakes, but that's all it is.

The problem with pointers, is that it is not all that difficult to make mistakes. People can set them to NULL, and so on.

References are just like pointers. They're just pointers in a fancy suit. It's just harder to make mistakes. You can't have NULL for example.

More than this, you'll need to go get a book - but I hope this has given you something to think about, and experiment with.

A reference
patilvaibhavrao 25-Mar-11 2:37am    
still i m confused
If You use call by value method in copy constructor then creation of object process will go in infinity loop.
Share this answer
patilvaibhavrao 22-Mar-11 6:57am    
how the process of object creation is goes in infinite please explain

If u passed by value to copy constructer,it will call itself,to copy the actual parameter to formal parameter,it will call a endless chain of call to copy constucter,this process would go on untill the system runs out of memory.
Share this answer
[no name] 26-Mar-14 14:30pm    
Do you think that it's possible that he got that.... 3 years ago in solution 2 and 3?
Hi here i am going to explain what i understood from the copy constructor theory.
See the following example
1   class AClass
2   {
3   public:
4       int val;
5       AClass()
6       {
7           cout<<"In Constructor"<<endl;
8       };
9       AClass(AClass objA)
10      {
11          cout<<"In Copy Constructor"<<endl;
12          val = objA.val;
13      }
14  };
16  int main()
17  {
18      AClass obA;
19      AClass obB(obA);
20  }

Assume there no compiler errors and warnings regarding to copy constructor.In the example copy constructor with call by value is declared in line 9.Now in line we are calling copy constructor function(you can assume it as function) with argument obA.So your obA has to be copied in order to pass it as argument, so again compiler has to call copy constructor.So this process will go into infinite loop.This was what i understood. Hope this will help you to think in your own way.

Removed an & in the "copy by value ctor prototype making it anymore a value ..."
Share this answer
patilvaibhavrao 23-Mar-11 1:55am    
sory i can't get

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900