Consider the following code:
#include <iostream>
#include <typeinfo>
#include <type_traits>
using namespace std;
struct A { int data; };
struct B1 : A {};
struct B2 : virtual A {};
struct Base1 : virtual A {};
struct Base2 : virtual A {};
struct Derived : Base1, Base2 {};
int main() {
cout << sizeof(B1) << endl;
cout << sizeof(B2) << endl;
cout << sizeof(Derived) << endl;
cout << std::is_polymorphic<B1>::value << endl;
cout << std::is_polymorphic<B2>::value << endl;
cout << std::is_polymorphic<Derived>::value << endl;
return 0;
}
On my system it prints
4
8
12
0
0
0
Which means that none of these classes is polymorphic. However, sizes of B1 and B2 differ by exactly size of a pointer, which is probably a pointer to vtable. I've ran gcc with -fdump-class-hierarchy
and got:
Vtable for B2
B2::_ZTV2B2: 3u entries
0 4u
4 (int (*)(...))0
8 (int (*)(...))(& _ZTI2B2)
VTT for B2
B2::_ZTT2B2: 1u entries
0 ((& B2::_ZTV2B2) + 12u)
Class B2
size=8 align=4
base size=4 base align=4
B2 (0x0x3ca5400) 0 nearly-empty
vptridx=0u vptr=((& B2::_ZTV2B2) + 12u)
A (0x0x3c972d8) 4 virtual
vbaseoffset=-12
Here is a question: what is a polymorphic class? Does 'having vtable' means 'be polymorphic and have RTTI' and vice-versa? If not, why do it have to have at least one virtual function to be polymorphic, if it's going to have vtable anyway?
Looks like definition of polymorphic class differs from 'the class that has a vtable', because, as we can see above, B2 is not a polymorphic, but has a vtable and, as I think, should have a RTTI. In this document it's clearly stated that "polymorphic - i.e. have at least one virtual function. (This is necessary to allow the generated dispatch code to use RTTI on the classes.)".
Only classes with virtual member functions and/or a virtual destructor have a vtable.
# Vtable is created by compiler at compile time. # VPTR is a hidden pointer created by compiler implicitly. # If base class pointer pointing to a function which is not available in base class then it will generate error over there. # Memory of Vtable and VPTR gets allocated inside the process address space.
Vptr and Vtable get stored in Data Segment... Vtable is like an array of function pointer. Vtable and Vptr is creating at compile time which will get memory in run time and vtable entries are virtual function addresses .
A virtual function is a special type of function that, when called, resolves to the most-derived version of the function that exists between the base and derived class. This capability is known as polymorphism.
You are missing a detail here: virtual tables are an implementation detail. As a result:
Thus, yes the compilers I know (MSVC and those following the Itanium ABI such as gcc, icc and Clang) will use virtual tables to provide the RTTI necessary for dynamic_cast
to work in the presence of virtual bases... and no this has nothing to do really with whether a class is polymorphic or not.
On a tangent, though, Prefer Composition Over Inheritance implies that there is little reason to inherit from a class if there is no behavior that can be overriden.
Here is a defenition of std::is_polymorphic
If T is a polymorphic class (that is, a class that declares or inherits at least one virtual function), provides the member constant value equal true. For any other type, value is false.
Since no functions are defined virtual, it will return false.
To elaborate, we should distinguish between polymorphism and vtables. In C++ vtables are indeed needed for polymorphism, but also for other non-polymorphic concepts too.
I could try to explain virtual inheritance, which when used with multiple inheritance will create a vtable, but this link does it better.
For those curious about the effects of the VTable provided by the inheritance.
The following sample
struct VB{
// virtual int f(){}
};
struct IA:virtual VB{};
int main(int argc, char** argv) {
VB* o = new IA;
IA* iap = dynamic_cast<IA*>(o);
}
will not compile (g++ 4.8):
main.cpp: In function ‘int main(int, char**)’: main.cpp:26:34: error: cannot dynamic_cast ‘o’ (of type ‘struct VB*’) to type ‘struct IA*’ (source type is not polymorphic) IA* iap = dynamic_cast(o);
While uncommenting the int f()
member gives the desired result.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With