Virtual member call in a constructor

http://stackoverflow.com/questions/119506/virtual-member-call-in-a-constructor

(Assuming you‘re writing in C# here)

When an object written in C# is constructed, what happens is that the initializers run in order from the most derived class to the base class, and then constructors run in order from the base class to the most derived class (see Eric Lippert‘s blog for details as to why this is).

Also in .NET objects do not change type as they are constructed, but start out as the most derived type, with the method table being for the most derived type. This means that virtual method calls always run on the most derived type.

When you combine these two facts you are left with the problem that if you make a virtual method call in a constructor, and it is not the most derived type in its inheritance hierarchy, that it will be called on a class whose constructor has not been run, and therefore may not be in a suitable state to have that method called.

This problem is, of course, mitigated if you mark your class as sealed to ensure that it is the most derived type in the inheritance hierarchy - in which case it is perfectly safe to call the virtual method.

http://blogs.msdn.com/b/ericlippert/archive/2008/02/18/why-do-initializers-run-in-the-opposite-order-as-constructors-part-two.aspx

Why Do Initializers Run In The Opposite Order As Constructors? Part Two

As you might have figured out, the answer to last week‘s puzzle is "if the constructors and initializers run in their actual order then an initialized readonly field of reference type is guaranteed to be non null in any possible call. That guarantee cannot be met if the initializers run in the expected order."

Suppose counterfactually that initializers ran in the expected order, that is, derived class initializers run after the base class constructor body. Consider the following pathological cases:

class Base
{
    public static ThreadsafeCollection t = new ThreadsafeCollection();
    public Base()
    {
        Console.WriteLine("Base constructor");
        if (this is Derived) (this as Derived).DoIt(); 
        // would deref null if we are constructing an instance of Derived
        Blah(); 
        // would deref null if we are constructing an instance of MoreDerived
        t.Add(this);
        // would deref null if another thread calls Base.t.GetLatest().Blah();
        // before derived constructor runs
    }
    public virtual void Blah() { }

class Derived : Base
{
    readonly Foo derivedFoo = new Foo("Derived initializer");
    public DoIt()
    {
        derivedFoo.Bar();
    } 
}
class MoreDerived : Derived
{
    public override void Blah() { DoIt(); }
}

Calling methods on derived types from constructors is dirty pool, but it is not illegal. And stuffing not-quite-constructed objects into global state is risky, but not illegal. I‘m not recommending that you do any of these things -- please, do not, for the good of us all. I‘m saying that it would be really nice if we could give you an ironclad guarantee that an initialized readonly field is always observed in its initialized state, and we cannot make that guarantee unless we run all the initializers first, and then all of the constructor bodies.

Note that of course, if you initialize your readonly fields in the constructor, then all bets are off. We make no guarantees as to the fields not being accessed before the constructor bodies run.

Next time on FAIC: how to get a question not answered.

时间: 2024-10-13 12:46:35

Virtual member call in a constructor的相关文章

C++对象模型——Virtual Member Functions (虚拟成员函数)(第四章)

4.2 Virtual Member Functions (虚拟成员函数) 已经看过了 virtual function的一般实现模型:每一个 class 有一个 virtual table,内含该 class 中有作用的 virtual function的地址,然后每个object有一个vptr,指向 virtual table的所在. 为了支持 virtual function机制,必须首先能够对多态对象有某种形式的"执行期类型判断法(runtime type resolution)&quo

Virtual Member Functions & Static Member Function

如果一个 normalize() 是一个 virtual member function, 那么以下的调用: ptr->normalize(); 将会被内部转化为: (*ptr->vptr[1])(ptr); 其中:vptr 表示由编译器生成的指针, 指向 virtual table, 它被安插在每一个声明有(或继承自) virtual functinos 的 class object 中. 事实上其名称也会被 mangled, 因为在一个复杂的 class 派生体系中, 可能存在多个 vpt

Virtual Member Functions 的语意

看过之前的 virtual function可以知道其实现模型: 每一个 class 有一个 virtual table. 内含该 class 之中 有作用的 virtual function 地址, 然后每个 object 有一个 vptr, 指向 virtual table 的所在. 在这一节中, 需要走访一组可能的设计, 然后根据单一继承, 多重继承和虚拟继承的多种情况, 从细节上来探究这个模型.为了支持 virtual function 机制, 必须首先能够对于多态对象有某种形式的执行期

C++对象模型——Member的各种调用方式(第四章)

第四章 Function语意学 (The Semantics of Function) 如果有一个Point3d的指针和对象: Point3d obj; Point3d *ptr = &obj; 当这样做: obj.normalize(); ptr->normalize(); 时,会发生什么事情呢?其中的Point3d::normalize()定义如下: Point3d Point3d::normalize() const { register float mag = magnitude()

C++对象模型——Default Constructor的建构操作(第二章)

第2章    构造函数语意学 (The Semantics of Constructor) 关于C++,最常听到的一个抱怨就是,编译器背着程序员做了太多事情.Conversion运算符就是最常被引用的一个例子. 2.1    Default Constructor的建构操作 C++ Annotated Reference Manual(ARM)指出"default constructors ...在需要的时候被编译器产生出来".关键字眼是"在需要的时候".被谁需要?

继承与 Data Member(2)

加上多态的情况如果我要处理一个坐标点, 而不在意这是一个 Point2d 或 Point3d 实例, 那么就需要在继承关系中提供一个 virtual function 接口: class Point2d { public: Point2d(float x = 0.0, float y = 0.0) :_x(x), _y(y){}; //x 和 y 的存取函数与前一个博客中相同 //由于对不同维度的点, 这些函数操作固定不变, 所以不必设为 virtual virtual float Z()(fl

(C/C++ )Interview in English - Virtual

Q: What is virtual function?A: A virtual function or virtual method is a function or method whose behavior can be overridden within an inheriting class by a function with the same signature. This concept is an important part of the polymorphism porti

Default Constructor的构建操作

在<C++ Annotated Reference Manual(ARM)[ELLIS90]>中的Section 12.1告诉我们:"Default constructors...在需要的时候被编译器产生出来". 其实默认构造函数也是分为两类的:有用的(nontrivial ).无用的(trivial ). 所谓有用的标准也是就默认构造函数会为我们的类做一些初始化操作.那么无用的就不会做任何工作,从而对我们的类也就没有任何意义. 所以,我们通常所说的默认构造函数是指有用的默

编译器会自动生成default constructor,这是真的吗?

相信很多人的回答都是"是的吧".Okay,是不是真的先不提了,先来看一个例子看看下面的代码能否编译通过,如果编译通过,能运行吗?如果不能运行,在哪出错? class foo { public: int val; foo *pNext; }; void Test1(){ foo f; if (f.val || f.pNext ){ cout << "in if" << endl; } } int main(){ Test1(); return