1.

Why & Sign Is Used In Copy Constructor?

Answer»

To avoid local copy of object reference (&) is used in copy constructor. 

Moreover if the & is omitted then copy constructor GOES in infinite loop.

eg. if class name is SAMPLE

Copy constructor WITHOUT &

Sample :: Sample (Sample s)

{

//Code goes here

}

and we create object as FOLLOWS. ;

Sample s;

Sample S1(s); 

In this scenario program will go in infinite loop.

To avoid local copy of object reference (&) is used in copy constructor. 

Moreover if the & is omitted then copy constructor goes in infinite loop.

eg. if class name is Sample. 

Copy constructor without &

Sample :: Sample (Sample s)

{

//Code goes here

}

and we create object as follows. ;

Sample s;

Sample s1(s); 

In this scenario program will go in infinite loop.



Discussion

No Comment Found