简体   繁体   English

C ++中的多态性,子类型?

[英]Polymorphism in C++ , sub-types?

can someone explain to me the last 5 lines , why that happen when Manager is sub-object to Employee ? 有人可以向我解释最后5行,为什么当Manager是Employee的子对象时会发生这种情况?

ostream& operator << (
   ostream &, const Employee &
);
Employee  e;
Manager   m;
Employee &eRef1 = e;  // OK!     
Employee &eRef2 = m;  // OK!  
Manager  &mRef1 = e;  // Compile error!
Manager  &mRef2 = m;  // OK!
cout << e << m;       // OK! 

That happens exactly because of what you said: Manager derives from Employee (I guess this is what you mean when you say " Manager is sub-object to Employee " ). 这恰好是因为你所说的: Manager来自Employee (我猜这就是你说ManagerEmployee子对象”的意思 )。 This means all instances of Manager are also instances of Employee , but not vice versa. 这意味着Manager所有实例也是Employee实例,但反之亦然。

Here, you are trying to bind a reference to a Manager to an object of type Employee . 在这里,您尝试将对Manager的引用绑定到Employee类型的对象。 But since an instance of Employee is not an instance of Manager (that's the other way round!) you get an error. 但是,由于Employee的一个实例不是Manager的实例(反之亦然!),您会收到错误。

If you want to understand why that's correct, try to think of what could happen if you did not get an error: 如果你想知道为什么这是正确的,再想想如果你没有得到一个错误会发生什么:

Employee e;
// ...
Manager& m = e; // This won't work, but let's suppose it did...
int s = m.get_size_of_managed_team(); // Huh?

If you could bind a reference to a Manager to an object which is not really a Manager , you may invoke functions for it that the actual object doesn't support. 如果你可以绑定一个引用到Manager到一个对象,它是不是一个真正的Manager ,你可以调用它的功能,实际的对象不支持。 This would be chaos. 这将是混乱。 Therefore, the compiler prevents this circumstance from arising at all. 因此,编译器可以防止出现这种情况。

References exhibit polymorphic behaviour. 参考文献表现出多态行为。 That is, you can have a reference to base class initialised with a derived class. 也就是说,您可以引用使用派生类初始化的基类。 However, you can't have a reference to derived class initialised with a base class. 但是,您不能引用使用基类初始化的派生类。

Since Employee is the base of Manager , you cannot initialise a Manager reference with an employee. 由于EmployeeManager的基础,因此您无法使用员工初始化Manager引用。 The rest of the initialisations are fine. 其余的初始化很好。

Reference | Initialised with | Valid?
----------|------------------|--------
Base&     | Base             | Yes
Base&     | Derived          | Yes
Derived&  | Base             | No
Derived&  | Derived          | Yes

This is intuitive. 这很直观。 Why would you allow any Employee to hide under the disguise of a Manager ? 为什么你会允许任何Employee隐藏在Manager的伪装下? Imagine working in a place like that - it'd be chaos! 想象一下,在这样的地方工作 - 它会很混乱!

In hierarchy like this, where classes are based off other classes, think of it this way. 在这样的层次结构中,类基于其他类,以这种方式考虑它。

A -> B -> C -> D A - > B - > C - > D.

In that example, let -> mean B inherits from A, etc. 在那个例子中,让 - >意味着B继承自A等。

If you think of it that way, classes can morph into the classes less than them(to the left): D can become C, B, or A. For B, Classes to the right are not accessible, since they're "higher" up the chain, therefore you couldn't make B into a C, or D. 如果你这样想,类会变成比它们少的类(左边):D可以变成C,B或A.对于B,右边的类是不可访问的,因为它们“更高” “上链,因此你不能把B变成C或D.

In your example, 在你的例子中,

Employee -> Manager 员工 - >经理

Managers can move left to Employee, so you can morph it that way, but Manager is to the RIGHT of Employee, which as said earlier, makes that transition impossible. 管理人员可以向左移动到员工,因此您可以通过这种方式进行转换,但管理员是员工的权利,如前所述,这使得转换变得不可能。

The reasoning is because, when you inherit, you gain all the benefits of the base class; 原因是,当你继承时,你获得了基类的所有好处; variables, functions, all of it. 变量,函数,所有这些。 When you try to go up the chain, you're adding variables, classes, etc, and you essentially have to re-create your object to do so, as it now takes more memory and will behave differently than originally. 当你试图上链时,你要添加变量,类等,你基本上必须重新创建你的对象才能这样做,因为它现在需要更多的内存,并且行为与最初不同。 But, going down the tree, you're essentially just stripping an onion; 但是,走下树,你基本上只是剥洋葱; taking off layers to reveal the spot of the onion you're after. 脱掉层来揭示你所追求的洋葱斑。 They're all there to begin with, they're just not directly visible since they make up the smaller parts. 它们都是从那里开始的,它们只是不直接可见,因为它们构成了较小的部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM