编译花絮:这是什么码结果 [英] Compiler Trivia: What is consequence of this code

查看:142
本文介绍了编译花絮:这是什么码结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在审查一些代码,今天遇到了一些代码(由这个片段精确地画出)...

I was reviewing some code today and came across some code (accurately portrayed by this snippet)...

public abstract class FlargBase{
    public FlargBase(){
        this.DoSomething();
    }

    public abstract void DoSomething();
}

public class PurpleFlarg: FlargBase{
    public PurpleFlarg()
      : base(){
    }

    public override void DoSomething(){
        // Do something here;
    }
}



编译器不提供任何错误或警告,但CodeAnalysis警告呼叫链中包含一个虚拟方法的调用,并可能会产生意想不到的结果。

The compiler gives no errors or warnings, but CodeAnalysis warns that the call chain contains a call to a virtual method and may produce unintended results.

我很好奇,因为,在我看来,两件事情都可能发生。

I was curious because, as I see it, two things can happen.


  1. 创建基类的实例,将一个方法调用没有实现定义。我希望编译器错误,或者运行时抛出一个异常,由于缺少实施。我假设编译器提供的一个实现{} 我打错原代码;它没有包含在类抽象的关键字。

  2. 创建一个派生类的实例会导致上没有实际尚未构建一个类的方法的调用。我本来期望这抛出异常。

  1. Creating an instance of the base class will make a call to a method with no defined implementation. I would expect the compiler to error, or the runtime to throw an exception due to a missing implementation. I'm assuming the compiler is providing an implementation of {} I mis-typed the original code; it did contain the abstract keyword on the class.
  2. Creating an instance of a derived class will cause a call to a method on a class that has not actually been constructed yet. I would have expected this to throw an exception.

此代码已经在生产环境中了好几个月。这显然是正常工作,以至于谁也没有注意到任何异常行为。

This code has been in a production environment for several months. It is apparently working correctly enough that no one has noticed any strange behavior.

我希望令人难以置信的天赋在这里计算器可以给我一些见解的行为和后果这些代码的。

I'm hoping the incredible talent here at StackOverflow can give me some insight into the behavior and consequences of this code.

推荐答案

C#对象被完全构造和第一个构造函数运行前初始化为零。该基地的构造将调用派生实现虚拟的方法。

A C# object is fully constructed and initialized to zero before the first constructor runs. The base constructor will call the derived implementation of the virtual method.

它被认为不好的风格要做到这一点,因为派生实现可能运行异常时,派生类的构造函数尚未被调用。但本身的行为被很好地定义。如果你什么都不做的派生实现,需要从构造函数的代码已经运行,它会奏效。

It's considered bad style to do this, because the derived implementation might behave strangely when the constructor of the derived class has not been called yet. But the behavior by itself is well defined. If you do nothing in the derived implementation that requires the code from the constructor to have already run, it will work.

您可以形象的运行时调用最派生的构造函数第一。而它的第一个动作是隐式调用基构造函数。我不知道,如果是这样的实际实施,但由于一些.NET语言,您可以致电派生的构造函数的任意点的基础构造,我希望C#简单地调用基类的构造函数派生的第一个动作构造函数。

You can image that the runtime calls the most derived constructor first. And its first action is to implicitly call the base constructor. I'm not sure if it's actually implemented like that, but since some .net languages allow you to call the base constructor at an arbitrary point of the derived constructor, I expect C# to simply call the base class constructor as first action of the derived constructor.

这行为是从C ++如何处理它很大的不同。在C ++中派生类得到构造一前一后,与派生类的构造函数之前已开始就仍然是基类的类型和派生类中的覆盖被忽略的对象。

This behavior is very different from how C++ handles it. In C++ the derived classes get constructed one after the other, and before the constructor of the derived class has started the object has still the type of the baseclass and the overrides from the derived class are ignored.

这篇关于编译花絮:这是什么码结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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