将派生类对象分配给父类引用

Assigning derived class object to a parent class reference(将派生类对象分配给父类引用)
本文介绍了将派生类对象分配给父类引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我看到时我总是很困惑:

I am always puzzled when I see:

Parent ref = new Child();

子类扩展父类的地方.

  1. 对象 ref 在内存中的样子如何?
  2. 虚方法如何处理?非虚拟?
  3. 与以下有何不同:
  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.

变量的存储位置通常实现为包含托管指针"的 4 或 8 字节整数——垃圾收集器已知的内存地址.

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.

对象的内存布局也是CLR的一个实现细节.与对象关联的内存缓冲区将包含对象的所有数据——字段的所有值等等.它还包含对另一个内存位置的引用,即对象的虚函数表.

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.

然后,虚函数表 (vtable) 包含更多的引用,这一次引用指向与对象的最衍生类型关联的方法.

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?

通过从变量中查找对象引用,然后查找 vtable,然后在 vtable 中查找方法,然后调用该方法来执行虚拟方法.

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.

非虚拟方法不会通过 vtable 调用,因为它们在编译时是已知的.

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

它与...有何不同

对象上调用的非虚方法会根据变量的类型调用方法的版本.对象上调用的虚方法会根据变量所引用的对象的类型调用方法的版本.

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

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)