using System;using System.Diagnostics;using System.Text;using System.Collections;using System.Collections.Generic;class Test{ public class Base { public Base() { System.Console.WriteLine("Base.Base"); ABitDangerousCall(); } public virtual void ABitDangerousCall() { System.Console.WriteLine("Base.ABitDangerousCall"); } private class Inner { public Inner() { System.Console.WriteLine("Base.Inner.Inner"); } } private Inner inner = new Inner(); } class Derived : Base { public Derived() { System.Console.WriteLine("Derived.Derived"); ctorInitializedMember = 5; } // ctorInitializedMember is default initialized to zero before the constructor initializes it. private int ctorInitializedMember; private int derivedInt; public override void ABitDangerousCall() { System.Console.WriteLine(String.Format("Derived.ABitDangerousCallctorInitializedMember={0} derivedInt={1}", ctorInitializedMember, derivedInt)); } private class Inner { public Inner() { System.Console.WriteLine("Derived.Inner.Inner"); } } private Inner inner = new Inner(); } class Program { static void Main(string[] args) { Derived d = new Derived(); Console.Read(); } }}
Derived.Inner.Inner
Base.Inner.InnerBase.BaseDerived.ABitDangerousCallctorInitializedMember=0 derivedInt=0Derived.Derived由于实例构造函数的调用顺序是先基类构造函数再子类构造函数,如上面例子,在基类中调用被子类重写了的虚函数ABitDangerousCall时,实际会调用子类的ABitDangerousCall,
该函数中使用了变量ctorInitializedMember,但此时子类的构造函数还没调用,故ctorInitializedMember未被赋值,这样就出现了不想看到的结果。我们本来是想想看到ctorInitializedMember=5的结果,结果却是0。
因此在基类的构造函数中调用虚函数是很危险的,项目大了代码复杂了就很容易造成极隐蔽的BUG。同时在子类的构造函数中调用虚函数也是要小心的,尽量避免,原因同上。
如果非要调用,那就要细心察看虚函数中的变量是否已经全部在前面处理妥当了