`

Item 9 避免在ctor和dtor中调用虚函数

阅读更多

看下面的程序有什么问题。

在基类的构造过程中,虚函数调用不会被传递到派生类中。因为基类的ctor是在派生类之前执行的,这时其数据成员还没有被初始化,即,派生对象还不存在!所以C++拒绝这样做。
不止虚函数,使用到RTTI的部分(dynamic_cast和typeid),在基类的ctor里,都不会被解析成派生类。
ctor和dtor同样。
一些编译器会对此给出警告。

● 注意,还有一种隐藏版本:

● 如果非要在基类里实现这样的功能,怎么办?下面是一种办法。

分享到:
评论

相关推荐

    C# 构造函数如何调用虚方法

    在C#中,用virtual关键字修饰的方法(属性、事件)称为虚方法(属性、事件),表示该方法可以由派生类重写(override)。虚方法是.NET中的重要概念,可以说在某种程度上,虚方法使得多态成为可能。 然而虚方法的使用...

    rust-ctor:Rust的模块初始化全局构造函数

    rust-ctor:Rust的模块初始化全局构造函数

    用c实现面向对象(oopc)

    系统和全面地介绍用c的方式实现面向对象编程。包括继承,虚函数,异常等处理 来自法国的牛人,采用宏的方式实现,形式如下: void constMethodDecl(print) { printf("name:\t%s\n", this->m.name); } ...

    v3ctor:基于研究的矢量场,发散和卷曲模拟

    要使用python启动仿真,请导航至v3ctor_main文件夹(通过使用终端命令“ cd file path”),然后使用命令“ python v3ctor.py”启动仿真。 在Windows上运行 如果您不想安装python,则可以下载Git存储库并将v3ctor....

    生成系统日志

    调用 exeQuery 接口时出现异常:System.IO.IOException: 该进程无法访问文件“e:\syslog\sells_log\att_log\2005\10\7\Att_log_2005_10_7_15.txt”,因为该文件正由另一进程使用。 at System.IO.__Error....

    child-process-ctor-Node.js4childProcess.ChildProcessponyfill.

    child-process-ctor - Node.js 4 childProcess.ChildProcess ponyfill.

    move_ctor.rar_in

    USET_SPAN_SIMPLE should be passed in for the start of src and USET_SPAN_NOT_CONTAINED should be passed in if we continue after an in-filter prefix.

    param_ctor.rar_Over

    atmclip.h - Classical IP over ATM.

    param_ctor.rar_The Media

    Please update media java android media MediaRecorder.java if the following is updated.

    param_ctor_init.rar_Cause

    Provide a real file - not a symlink - as it would cause multiarch conflicts when multiple different arch releases are installed simultaneously.

    param_ctor_default.rar_back

    Plays back commands the client. Returns true if any commands were processed for Linux v2.13.6.

    param_ctor_iterator.rar_Modified

    NOTE: This file is a standard OpenMAX AL header file and should not be modified in any way.

    C#中在WebClient中使用post发送数据实现方法

    很多时候,我们需要使用C#中....Ctor 包括 一个空构造函数 和一个静态构造函数, 静态构造函数主要为UrlEncode 和UrlEncodeAndWirte 编码提供参照byte[]数据的初始化作用。如下所示: stati WebClient() public WebCl

    ctor_char32_t.rar_memory

    Display target for Xlib winsys. Low-level OS window system memory buffer.

    ctor_move_alloc.rar_Alone!

    This test purpose is simply to check Standard header independancy that is to say that the header can be included alone without any previous include.

    C++内存管理1

    1. new expression 确实 先调用 operator new () 再调用 ctor 2. delete expression 先调用 dtor

    ctor_char16_t.rar_The Other

    All other types should have already been filtered by other paths in the caller.

    bad_function_call_ctor.rar_BAD

    DOC: uevents generated by i915 on it s device node for linux Device Driver.

    param_ctor_func.rar_Make It!

    Make sure it clobbers something.

    如何通过IL了解C#类的构造函数浅析

    那么,问题来了,为什么在调用构造函数前会初始化类中的字段和属性呢?让我们一起通过ildasm来揭开构造函数的面纱吧。 需要反编译的C#代码如下: class CtorTester { private string _name; private int _age = ...

Global site tag (gtag.js) - Google Analytics