派生类与基类间的赋值兼容性
在任何需要基类对象的地方都可以用公有派生类的对象来代替,这条规则称赋值兼容规则。
它包括以下情况:
- 派生类的对象可以赋值给基类的对象,这时是把派生类对象中从对应基类中继承来的成员赋值给基类对象。反过来不行,因为派生类的新成员无值可赋。
- 可以将一个派生类的对象的地址赋给其基类的指针变量,但只能通过这个指针访问派生类中由基类继承来的成员,不能访问派生类中的新成员。同样也不能反过来做。
- 派生类对象可以初始化基类的引用。引用是别名,但这个别名只能包含派生类对象中的由基类继承来的成员
私有派生和保护派生不具备赋值兼容性 !
示例代码: ( 代码测试:http://codepad.org/V8uaM2Al )
1 #include <iostream> 2 3 using std::cout ; 4 using std::endl ; 5 6 class Base 7 { 8 public : 9 Base( ) { cout << "Create the Base Obj !" << endl ; } 10 11 ~Base( ) { cout << "Destroy the Base Obj !" << endl ; } 12 13 Base& operator=( const Base& ) 14 { 15 cout << "Assignment Operator occur to the Base Obj !" << endl ; 16 return *this ; 17 } 18 19 } ; 20 21 class PublicInherit : public Base 22 { 23 public : 24 PublicInherit( ) { cout << "Create the PublicInherit Obj !" << endl ; } 25 26 ~PublicInherit( ) { cout << "Destroy the PublicInherit Obj !" << endl ; } 27 28 } ; 29 30 class PrivateInherit : private Base 31 { 32 public : 33 PrivateInherit( ) { cout << "Create the PrivateInherit Obj !" << endl ; } 34 35 ~PrivateInherit( ) { cout << "Destroy the PrivateInherit Obj !" << endl ; } 36 37 } ; 38 class ProtectInherit : protected Base 39 { 40 public : 41 ProtectInherit( ) { cout << "Create the ProtectInherit Obj !" << endl ; } 42 43 ~ProtectInherit( ) { cout << "Destroy the ProtectInherit Obj !" << endl ; } 44 45 } ; 46 47 int main( int argc, char **argv ) 48 { 49 cout << "--------------- Create Obj ! ---------------" << endl ; 50 Base obj_base ; 51 Base *p_base = 0 ; 52 53 PublicInherit *p_public = new PublicInherit( ); 54 PrivateInherit *p_private = new PrivateInherit( ); 55 ProtectInherit *p_protect = new ProtectInherit( ); 56 57 cout << endl << "-------- Assignment Occur to Obj ! --------" << endl ; 58 59 obj_base = *p_public ; 60 61 //obj_base = *p_private ; // error: 'Base' is an inaccessible base of 'PrivateInherit' 62 63 //obj_base = *p_protect ; // error: 'Base' is an inaccessible base of 'ProtectInherit' 64 65 cout << endl << "------ Assignment Occur to Pointer ! -------" << endl ; 66 67 p_base = p_public ; 68 69 //p_base = p_private ; // error: 'Base' is an inaccessible base of 'PrivateInherit' 70 71 //p_base = p_protect ; // error: 'Base' is an inaccessible base of 'ProtectInherit' 72 73 cout << endl << "-------------- Destroy Obj ! ---------------" << endl ; 74 75 delete p_protect ; 76 delete p_private ; 77 delete p_public ; 78 79 80 }
运行结果:
1 --------------- Create Obj ! --------------- 2 Create the Base Obj ! 3 Create the Base Obj ! 4 Create the PublicInherit Obj ! 5 Create the Base Obj ! 6 Create the PrivateInherit Obj ! 7 Create the Base Obj ! 8 Create the ProtectInherit Obj ! 9 10 -------- Assignment Occur to Obj ! -------- 11 Assignment Operator occur to the Base Obj ! 12 13 ------ Assignment Occur to Pointer ! ------- 14 15 -------------- Destroy Obj ! --------------- 16 Destroy the ProtectInherit Obj ! 17 Destroy the Base Obj ! 18 Destroy the PrivateInherit Obj ! 19 Destroy the Base Obj ! 20 Destroy the PublicInherit Obj ! 21 Destroy the Base Obj ! 22 Destroy the Base Obj !