05-02-2011, 05:36 PM
There are 3 important places where a copy constructor is called.
When an object is created from another object of the same type
When an object is passed by value as a parameter to a function
When an object is returned from a function
A variable is declared which is initialized from another object, eg,
· Person q("Mickey"); // constructor is used to build q.
· Person r(p); // copy constructor is used to build r.
· Person p = q; // copy constructor is used to initialize in declaration.
· p = q; // Assignment operator, no constructor or copy constructor.
A value parameter is initialized from its corresponding argument.
f(p); // copy constructor initializes formal value parameter.
An object is returned by a function.
C++ calls a copy constructor to make a copy of an object in each of the above cases. If there is no copy constructor defined for the class, C++ uses the default copy constructor which copies each field, ie, makes a shallow copy.
When an object is created from another object of the same type
When an object is passed by value as a parameter to a function
When an object is returned from a function
A variable is declared which is initialized from another object, eg,
· Person q("Mickey"); // constructor is used to build q.
· Person r(p); // copy constructor is used to build r.
· Person p = q; // copy constructor is used to initialize in declaration.
· p = q; // Assignment operator, no constructor or copy constructor.
A value parameter is initialized from its corresponding argument.
f(p); // copy constructor initializes formal value parameter.
An object is returned by a function.
C++ calls a copy constructor to make a copy of an object in each of the above cases. If there is no copy constructor defined for the class, C++ uses the default copy constructor which copies each field, ie, makes a shallow copy.
0300-9520262
Get solution in 24 hour |cpmputer & internet
mileys81:[SIZE="4"][SIZE="3"]....[/SIZE][/SIZE]
