Or, are there any other known negative affects of employing __declspec(novtable)? I can't seem to find references to any issues.
Does using __declspec(novtable) on abstract base classes affect RTTI in any way?
8.2k views Asked by oz10 At
2
There are 2 answers
0
On
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.
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.
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.Note: virtual f()
= 0
; makes f to be apure virtual function
and B to be an abstract class.The
definition
of a pure virtual functioncould
(notmust
) be missing.C++ allows virtual function call in constructor which we don't recommend.
Update: A mistake in D2 : the vptr in derived constructor.
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.