在抽象基类上使用 __declspec(novtable) 是否会以任何方式影响 RTTI?

发布于 2024-08-12 16:39:50 字数 62 浏览 7 评论 0原文

或者,使用 __declspec(novtable) 是否还有其他已知的负面影响?我似乎找不到任何问题的参考。

Or, are there any other known negative affects of employing __declspec(novtable)? I can't seem to find references to any issues.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

百合的盛世恋 2024-08-19 16:39:50

MSCV 使用每个对象一个 vptr,每个类一个 vtbl 来实现 OO 机制,例如 RTTI 和虚函数。
因此,当且仅当 vptr 设置正确时,RTTI 和虚拟函数才能正常工作。

struct __declspec(novtable) B {
    virtual void f() = 0;
};
struct D1 : B {
    D1() {
    }       // after the construction of D1, vptr will be set to vtbl of D1.
};
D1 d1;      // after d has been fully constructed, vptr is correct.
B& b = d1;  // so virtual functions and RTTI will work.
b.f();      // calls D1::f();
assert( dynamic_cast<D1*>(&b) );
assert( typeid(b) == typeid(D1) );

使用__declspec(novtable)时B应该是一个抽象类。
除了 D1 的构造函数之外,不会有 B 的实例。
并且 __declspec(novtable) 在大多数情况下没有负面影响。

但在构造派生类时__declspec(novtable)会使其与ISO C++语义不同。

struct D2 : B {


    D2() {  // when enter the constructor of D2 \  
            //     the vtpr must be set to vptr of B \
            //     if  B didn't use __declspec(novtable).
            // virtual functions and RTTI will also work.

            this->f(); // should calls B::f();
            assert( typeid(*this) == typeid(B) );
            assert( !dynamic_cast<D2*>(this) );
            assert( dynamic_cast<B*>(this) );

            // but __declspec(novtable) will stop the compiler \
            //    from generating code to initialize the vptr.
            // so the code above will crash because of uninitialized vptr.
    }
};

注意:virtual f() = 0;使 f 成为一个纯虚函数,并使 B 成为一个抽象类。
纯虚函数的定义可能(不是必须)丢失。
C++ 允许在构造函数中调用虚函数,但我们不推荐这样做。

更新:
D2 中的一个错误:派生构造函数中的 vptr。

struct D3 : B {  // ISO C++ semantic
    D3() {       // vptr must be set to vtbl of B before enter
    }            // vptr must be set to vtbl of D2 after leave
};

但vptr在构造过程中是不确定的,这也是不建议在构造函数中调用虚函数的原因之一。

如果D2::D2()中的vptr是B,并且缺少B::f()的定义, this->f(); 在 vtbl 中取消引用函数指针时会崩溃。
如果 D2::D2() 中的 vptr 是 B 并且 B 使用 novtable,则当取消引用未初始化的 vptr 时,this->f(); 将会崩溃。

事实上,D2:: 中的 vptr D2() 是 MSVC(msvc8) 中的 D2。编译器在执行 D2::D2() 中的其他代码之前将 vptr 设置为 D2。
因此 this->f(); 调用 D2::f() 并且这三个断言将被违反。

MSCV uses one vptr per object and one vtbl per class to implement OO mechanism such as RTTI and virtual functions.
So RTTI and virtual functions will work fine if and only if the vptr has been set correctly.

struct __declspec(novtable) B {
    virtual void f() = 0;
};
struct D1 : B {
    D1() {
    }       // after the construction of D1, vptr will be set to vtbl of D1.
};
D1 d1;      // after d has been fully constructed, vptr is correct.
B& b = d1;  // so virtual functions and RTTI will work.
b.f();      // calls D1::f();
assert( dynamic_cast<D1*>(&b) );
assert( typeid(b) == typeid(D1) );

B should be an abstract class when use __declspec(novtable).
There will be no instance of B except in the constructor of D1.
And __declspec(novtable) has no negative affects in most case.

But during the construction of derived class __declspec(novtable) will make it different from ISO C++ semantic.

struct D2 : B {


    D2() {  // when enter the constructor of D2 \  
            //     the vtpr must be set to vptr of B \
            //     if  B didn't use __declspec(novtable).
            // virtual functions and RTTI will also work.

            this->f(); // should calls B::f();
            assert( typeid(*this) == typeid(B) );
            assert( !dynamic_cast<D2*>(this) );
            assert( dynamic_cast<B*>(this) );

            // but __declspec(novtable) will stop the compiler \
            //    from generating code to initialize the vptr.
            // so the code above will crash because of uninitialized vptr.
    }
};

Note: virtual f() = 0; makes f to be a pure virtual function and B to be an abstract class.
The definition of a pure virtual function could (not must) be missing.
C++ allows virtual function call in constructor which we don't recommend.

Update:
A mistake in D2 : the vptr in derived constructor.

struct D3 : B {  // ISO C++ semantic
    D3() {       // vptr must be set to vtbl of B before enter
    }            // vptr must be set to vtbl of D2 after leave
};

But vptr is indeterminate during the construction.It's one of the reason that virtual function call in constructor aren't recommend .

If vptr in D2::D2() was B and definition of B::f() was missing, this->f(); will crash when dereference pointer-to-function in the vtbl.
If vptr in D2::D2() was B and B use novtable, this->f(); will crash when dereference an uninitialized vptr.

In fact , vptr in D2::D2() is D2 in MSVC(msvc8).The compiler set vptr to D2 before execute other code in D2::D2().
So this->f(); calls D2::f() and the three assertions will be violated.

陌上青苔 2024-08-19 16:39:50

如果我理解正确的话:
ctor 或 dtor 内的任何虚拟 fn 调用都会转换为编译时链接。我们无法从 (c/d)tor 进行虚拟 fn 调用。原因是,在创建基类的对象时,它不知道派生类,因此无法调用派生类,并且适用相同的逻辑。

if I understand it correctly:
Any virtual fn call inside a ctor or a dtor is converted to a compile time linking. We cannot make virtual fn calls from the (c/d)tors. The reason is that at the time the object of the base class is getting created it has no knowledge of the derived class and hence cannot make the call to derived class and w.r.t the dtors the same logic applies.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文