A virtual table for a derived class that has no virtual function other than the parent class, a virtual function

A virtual table will be created for a derived class that has no virtual function other than the parent class, a virtual function that is not overridden by the derived class.

for ex:

class A{ public: virtual void show(); }; class B : public A { }; 

What about a class B virtual table.

+5
source share
2 answers

You can verify this by viewing the contents of the object. I wrote this simple program that prints the contents of a base class, a derived class, and a class that matches the base class, but with the usual method instead of the virtual one:

 #include <iostream> #include <string> #include <iomanip> using namespace std; class Base { public: virtual void show() {} }; class Derived : public Base { }; class NonVirtual { public: void show() {} }; struct Test { int data1, data2; }; template <typename T> void showContents(T* obj, string name) { Test* test = new Test{}; test = reinterpret_cast<Test*>(obj); cout << name << ": " << hex << "0x" << test->data1 << " " << "0x" << test->data2 << endl; delete test; } int main() { Base* base = new Base{}; Derived* derived = new Derived{}; NonVirtual* nonVirtual = new NonVirtual{}; showContents(base, "Base"); showContents(derived, "Derived"); showContents(nonVirtual, "NonVirtual"); delete base; delete derived; delete nonVirtual; } 

Live demo


The result of executing the above program after compiling with cpp.sh (I'm not sure which compiler is used there):

 Base: 0x4013e0 0x0 Derived: 0x401400 0x0 NonVirtual: 0x0 0x0 

so I expect this to mean that a virtual table was actually created for the Derived object (at least for this compiler), since the required behavior is not defined in the C ++ standard).

0
source

There is no standard answer to your question. It really depends on the version of the compiler. In C ++, there is no standard ABI. If you're interested in deeper, take a look at "Itanium C ++ ABI" or try to find the answer yourself by looking at the assembler code.

There was even a proposal to define portable ABI for C ++

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4028.pdf

+2
source

All Articles