虚拟化技术在超类的构造函数 [英] Virtualization in Super Class Constructor

查看:124
本文介绍了虚拟化技术在超类的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的意见是,在超类的构造函数按照面向对象的设计虚拟化不工作的。例如,考虑下面的C#code。

I was of the opinion that virtualization doesnt work in the super class constructor as per the design of OOP. For example, consider the following C# code.

using System;
namespace Problem
{
    public class BaseClass 
    {
        public BaseClass() 
        {
            Console.WriteLine("Hello, World!");
            this.PrintRandom();
        }
        public virtual void PrintRandom() 
        {
            Console.WriteLine("0");
        }
    }

    public class Descendent : BaseClass 
    {
        private Random randomValue;
        public Descendent() 
        {
            Console.WriteLine("Bonjour, Monde!");
            randomValue = new Random();
        }
        public override void PrintRandom() 
        {
            Console.WriteLine(randomValue.NextDouble().ToString());
        }

        public static void Main() 
        {
            Descendent obj = new Descendent();
            obj.PrintRandom();
            Console.ReadLine();
        }
    }
}

这code休息,因为当后裔的对象制成,它调用基类的构造函数,我们有一个虚拟方法调用基类构造函数,再调用派生类的方法,因此,它因为randomValue崩溃是不是到时候intialized。

This code breaks because when the object of Descendent is made, it calls the base class constructor and we have a virtual method call in Base Class constructor which in turn calls the Derived class's method and hence, it crashes since randomValue is not intialized by that time.

一个类似code工作在C ++中因为调用PrintRandom不会被路由到国际海事组织以来派生类,在C ++中的顺序是这样的:

A similar code works in C++ because the call to PrintRandom is not routed to the derived class since IMO, the order in C++ is something like:


1。呼吁基类的构造函数
2。更新v - 输出表这个类
3。调用构造函数code


1. call for base class constructor
2. Update V - Table for this class
3. call the constructor code

我的问题是,我首先是否是正确的,根据面向对象的原则,虚拟化不能/不会在超类的构造函数的工作;其次,如果我是正确的,那么为什么行为在所有.NET语言不同(我曾与测试它的C#,VB.NET和MC ++)

My Question is that firstly whether I am right that as per OOP principles, virtualization shouldn't/doesn't work in the super class constructor and secondly if I am right, then why the behavior is different in all .NET languages ( I have tested it with C#, VB.NET and MC++)

推荐答案

在本地C ++,程序按预期工作:你调用内部的基类构造函数的基类版本的虚拟功能。在构造函数调用的时候,只有基类和虚函数存在,所以你得到的时候定义的虚拟函数的最低级版本。这并不意味着虚拟化不能用,你就不会得到在基类的构造函数虚函数子类的版本(这就是为什么它不推荐)。

In native C++, the program works as expected: you get the call to the base class version of the virtual function within the base class constructor. At the time of the constructor call, only the base class and its virtual functions exist, so you get the lowest-level version of the virtual function defined at the time. This does not imply that virtualization cannot be used, you just won't get the subclass versions of virtual methods in the constructors of base classes (which is why it's not recommended).

很明显,你可以看到,管理code的工作方式不同,因为(IIRC)的整个物体是建立在构造函数的调用之前,这样的话你得子类的构造函数之前的子类的虚函数。这是语言行为之间的文件差异,但应该是在整个.NET语言保持一致(因为它们都编译成相同的IL)。

Obviously, as you can see, managed code works differently, because (iirc) the entire object is built before the constructors are called, and thus you get the subclass virtual function before the subclass constructor. This is a documented difference between the behaviors of the languages, but should be consistent across .NET languages (since they all compile to the same IL).

这篇关于虚拟化技术在超类的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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