☰
  • 首页
  • 规则分类
  • 项目介绍
search
•••

typeid 作用于正在构造或析构的对象,而且该对象与当前构造或析构函数不属于同一个类或基类

C++-Undefined-Behavior-64
目录 › next › previous

示例:

struct V {
    virtual void foo();
};

struct A: virtual V {};
struct B: virtual V { B(V*, A*); };

struct C: A, B {
    C(): B((A*)this, this) {}
};

B::B(V* v, A* a) {
    typeid(*v);        // Well-defined, V is the base of B
    typeid(*a);        // Undefined behavior, A is not a base of B
}

依据

ISO/IEC 14882:2003 12.7(4)-undefined ISO/IEC 14882:2011 12.7(5)-undefined
Copyright©2024 360 Security Technology Inc., Licensed under the Apache-2.0 license.