多级继承中的多态性

时间:2017-05-23 08:19:58

标签: c++

以下代码中的派生类Test2和Test3不是虚拟类型,那么为什么要覆盖这些函数以及如何阻止它发生? P.S - >我是新手,所以这个问题可能是愚蠢的抱歉。

public class Service : BaseEntity
    {
        public int ServiceID { get; set; }
        public int RequestID { get; set; }
        public string BusinessLineCode { get; set; }
        public string BusinessLine { get; set; }
        public bool IsPrimaryBusinessLine { get; set; }
        public int ContractLineSLAID { get; set; }
}

1 个答案:

答案 0 :(得分:1)

Test2::printvirtual,因此在指向print的指针上调用Test2或从Test2派生的类指针将使用对象的运行时类型,用于确定要调用的函数。在您的情况下,对象的运行时类型为Test4,因此它将调用Test4::print。这就是virtual函数的重点。

如果您想明确调用print的其他版本,则可以执行此操作,例如test3->Test3::print();