A copy constructor is a special constructor in the C++ programming language for creating a new object as a copy of an existing object. The first argument of such a constructor is a reference to an object of the same type as is being constructed (const or non-const), which might be followed by parameters of any type (all having default values).Examples of these are:
cclass(const cclass& copy)
{
this->num = copy.num;
}The following cases may result in a call to a copy constructor:
1. When an object is returned by value
cclass CC = getClass(num);2. When an object is passed (to a function) by value as an argument
foo(CC);3. When an object is thrown
throw CC;4. When an object is caught
catch (CC)
And, to finish off, I need a full example.
////*class*////
Point::Point(const Point& p) {
x = p.age;
y = p.num;
}////*Main*////
Point p; // calls default constructor
Point s = p; // calls copy constructor.
p = s; // assignment, not copy constructor, need to overload the "=" operator
Комментариев нет:
Отправить комментарий