Or, are there any other known negative affects of employing __declspec(novtable)? I can't seem to find references to any issues.
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- How to know full paths to DLL's from .csproj f
- Importing NuGet references through a local project
相关文章
- How to show location of errors, references to memb
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- How to track MongoDB requests from a console appli
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
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.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.