C++ 拷贝控制和继承
2015-03-16 18:53
169 查看
The synthesized operations copy, assign, or destroy the base-class part of the object along with the members of the derived part.
Whether a class needs to define the copy-control members depends entirely on the class' own direct members.A baseclass might define its own copy control while the
derived uses the synthesize dversions or vice versa.
Classes with pointer members often need to define their own copy control to manage these members.
Note: If a derived class explicitly definesits own copy constructor or assignment operator, that definition completely overrides the defaults.The copy constructor and assignment
operator for inherited classes are responsible for copying or assigning their base class components as well as the members in the class itself.
Defininga Derived Copy Constructor
If a derived class defines its own copyconstructor, that copy constructor usually should explicitly use the base-classcopy constructor to initialize the base part of the object:
class Base { /* ... */ };
class Derived: public Base {
public:
// Base::Base(const Base&) not invoked auto matically
Derived(const Derived& d):
Base(d) /* other member initialization*/ {/*... */ }
};
Derived-ClassAssignment Operator
As usual, the assignment operator is similar to the copy constructor:If the derived class defines its ownassignment operator, then that operator must assign the base part
explicitly:
// Base::operator=(const Base&) notinvoked automatically
Derived &Derived::operator=(constDerived &rhs)
{
if (this != &rhs) {
Base::operator=(rhs); // assigns the basepart
// do whatever needed to clean up the oldvalue in the derived part
// assign the members from the derived
}
return *this;
}
Theassignment operator must, as always, guard against self-assignment. Assuming the leftand right-hand operands differ, then we call theBaseclass assignment operator
to assign the base-class portion. That operator might bedefined by the class or it might be the synthesized assignment operator.
It doesn't matterwecan call it directly. The base-class operator will free the old value in the base part of theleft-hand operand and will assign the new values from rhs. Once
that operator finishes, we continuedoing whatever is needed to assign the members in the derived class.
Derived-Class Destructor
The destructor works differently from thecopy constructor and assignment operator: The derived destructor is never responsible for destroying the members of its base objects.
The compiler always implicitly invokes the destructor for the basepart of a derived object. Each destructor does only what is necessary to clean up its own members:
class Derived: public Base {
public:
// Base::~Base invoked automatically
~Derived() { /* do what it takes to clean upderived members*/ }
}
Whether a class needs to define the copy-control members depends entirely on the class' own direct members.A baseclass might define its own copy control while the
derived uses the synthesize dversions or vice versa.
Classes with pointer members often need to define their own copy control to manage these members.
Note: If a derived class explicitly definesits own copy constructor or assignment operator, that definition completely overrides the defaults.The copy constructor and assignment
operator for inherited classes are responsible for copying or assigning their base class components as well as the members in the class itself.
Defininga Derived Copy Constructor
If a derived class defines its own copyconstructor, that copy constructor usually should explicitly use the base-classcopy constructor to initialize the base part of the object:
class Base { /* ... */ };
class Derived: public Base {
public:
// Base::Base(const Base&) not invoked auto matically
Derived(const Derived& d):
Base(d) /* other member initialization*/ {/*... */ }
};
Derived-ClassAssignment Operator
As usual, the assignment operator is similar to the copy constructor:If the derived class defines its ownassignment operator, then that operator must assign the base part
explicitly:
// Base::operator=(const Base&) notinvoked automatically
Derived &Derived::operator=(constDerived &rhs)
{
if (this != &rhs) {
Base::operator=(rhs); // assigns the basepart
// do whatever needed to clean up the oldvalue in the derived part
// assign the members from the derived
}
return *this;
}
Theassignment operator must, as always, guard against self-assignment. Assuming the leftand right-hand operands differ, then we call theBaseclass assignment operator
to assign the base-class portion. That operator might bedefined by the class or it might be the synthesized assignment operator.
It doesn't matterwecan call it directly. The base-class operator will free the old value in the base part of theleft-hand operand and will assign the new values from rhs. Once
that operator finishes, we continuedoing whatever is needed to assign the members in the derived class.
Derived-Class Destructor
The destructor works differently from thecopy constructor and assignment operator: The derived destructor is never responsible for destroying the members of its base objects.
The compiler always implicitly invokes the destructor for the basepart of a derived object. Each destructor does only what is necessary to clean up its own members:
class Derived: public Base {
public:
// Base::~Base invoked automatically
~Derived() { /* do what it takes to clean upderived members*/ }
}
相关文章推荐
- c++继承体系中的拷贝控制成员的定义
- c++继承中的构造函数与拷贝控制
- C++访问控制-三种继承方式
- C++复习之类继承的控制访问
- 【c++】拷贝控制具体分析
- C++的那些事:类的拷贝控制
- C++的继承控制
- C++ 访问控制和继承private、public、protected
- C++ 公有继承、保护继承和私有继承中类成员的访问权限的控制
- C++学习之继承中的成员访问控制
- [C++]访问控制与继承(public,protect,private) 有时间再整理!!!
- C++复习之类继承的控制访问
- 对C++保护继承的访问控制进行验证
- C++学习之继承中的访问控制
- C++的那些事:类的拷贝控制
- C++复习之类继承的控制访问
- C++访问控制与继承
- C++访问控制和继承关系
- C++继承中的访问控制实例分析
- C++学习之继承中的访问控制