2 replies
by tehilinski
A copy constructor is used to create an instance from another instance, e.g.:
MyClass A;
MyClass B (A);
When B is instantiated, the following constructor is called:
B (B const & object);
Use the body of the copy constructor to copy member data from the source object. e.g.:
class Ints
{
int x, y;
Ints (Ints const & object)
{
x = object.x;
y = object.y;
}
};
by eziogava
The copy constructor implements the construction of an instance when another one is given. If you implement the copy constructor you should also implement the equal operator with the same semantic. The copy constructor is used in all the situations where a copy of an instance is needed, for instance when passing parameters by value to a function or when returning an object from a function. Cloning an object may or may not be done by using the same semantic of the copy constructor. This depends if you require a shallow copy or a deep copy in the Clone() method. Cloning is best implemented by realizing a proper interface.
Best Regards
eziogava@angloirishbank.ie
This discussion is closed to new replies.
Trending Web Development posts
Sort by Trending