GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

  GeekInterview.com  >  Tech FAQs  >  OOPS

 Print  |  
Question:  Why the copy constructor is having a (&) variable as parameter?



January 01, 2008 21:18:29 #2
 irfan.omair   Member Since: January 2008    Total Comments: 2 

RE: Why the copy constructor is having a (&) variable as parameter?
 

It can be better explained in this way also.

Thumb Rule When a copy constructor is called ? : when any object is created and it is being initialized with otherObject of same type Copy constructor  is called. Blv me this is is only one way how copy constructor is called.

Now lets understand one thing with an example .

int foo ( int age) { } and this function is called from main () { int m=10; foo(m); }
now here in function foo(int age) , the varible age is being created and initialiezed with another value which is being send from main with name "m" .
similarly in the case of copyConstructor

class A { ..... A(A&a){}..}
if A(A&a) would be  A(A a) , then here in A(A a) an object a is being created and initialiezed with anoher object . and it will again call copy constructor as per the thumb Rule above. then again same story and this will continue untill the applicationsl stack is not overflowed. and program will fianaly crash.

     

 

Back To Question