分配派生类对象给父类的引用 [英] Assigning derived class object to a parent class reference

查看:143
本文介绍了分配派生类对象给父类的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我始终不解,当我看到:

 父REF =新的儿童(); 

其中儿童类继承父。




  1. 如何对象 REF 看起来像内存?

  2. 如何虚方法治疗? ?非虚

  3. 它是如何不同:





  REF儿童=新儿童(); 


解决方案

如何对象看在内存?




您的问题还不清楚。有两个相关的存储单元。在可变的与存储位置相关联。该存储位置包含的参考的到的其他的存储位置。



变量的存储位置通常被实现为一个包含管理指针一个四个或八个字节整数 - 已知的垃圾收集器内存地址。



对象的内存布局也是CLR的实现细节。与对象相关联的存储器缓冲器将包含该对象的所有数据 - 中的字段和诸如此类的所有的值。它还包含对尚未的其他的内存位置,在虚函数表的对象。



虚函数表(虚函数表),那么包含的更是引用的,这个时候引用引用最派生类型相关联的方法对象。




如何虚方法治疗?非虚?




虚拟方法通过查找从可变对象引用,然后仰视vtable中,然后在仰视方法执行虚函数表,然后调用该方法。



非虚方法不是通过虚表调用,因为他们是在编译时已知的。




它是如何从...


不同

非虚方法调用上对象将调用基础上的变量的类型的版本的方法。被称为对象的虚拟方法将调用基础上的的对象的该变量引用。



如果该类型版本的方法是不是所有的清晰,可能需要阅读我的文章,解释你如何可能效仿的语言虚拟方法不具有它们。如果你能理解如何自己实现虚拟方法在没有他们的语言,这将有助于您了解我们如何真正的的实现虚拟方法。



http://blogs.msdn.com/b/ericlippert/archive/2011/03/17/implementing-the-virtual-method-pattern-in-c-part-one.aspx


I am always puzzled when I see:

Parent ref = new Child();

where Child class extends Parent.

  1. How does the object ref look like in memory?
  2. How is virtual method treated? non-virtual?
  3. How is it different from:

Child ref = new Child();

解决方案

How does the object look in memory?

Your question is unclear. There are two relevant memory locations. The variable is associated with a storage location. That storage location contains a reference to another storage location.

The variable's storage location is typically realized as a four or eight byte integer that contains a "managed pointer" -- a memory address known to the garbage collector.

The object's memory layout is also an implementation detail of the CLR. The memory buffer associated with the object will contain all the data for the object -- all the values of the fields and whatnot. It also contains a reference to yet another memory location, the virtual function table of the object.

The virtual function table (vtable) then contains even more references, this time references that refer to the methods associated with the most-derived type of the object.

How is virtual method treated? non-virtual?

Virtual methods are executed by looking up the object reference from the variable, then looking up the vtable, then looking up the method in the vtable, and then invoking that method.

Non-virtual methods are not invoked via the vtable because they are known at compile time.

How is it different from...

Non-virtual methods called on the object will call the version of the method based on the type of the variable. Virtual methods called on the object will call the version of the method based on the type of the object that the variable refers to.

If that is not all clear, you might want to read my article that explains how you might "emulate" virtual methods in a language that does not have them. If you can understand how to implement virtual methods yourself in a language that does not have them, that will help you understand how we actually do implement virtual methods.

http://blogs.msdn.com/b/ericlippert/archive/2011/03/17/implementing-the-virtual-method-pattern-in-c-part-one.aspx

这篇关于分配派生类对象给父类的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆