3

Does every class have virtual function table in C++?

I know virtual table is for polymorphism. Class with virtual functions must have v-table. But how about class has no virtual function? Or how about class has no base class?

Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
Anders Lind
  • 4,542
  • 8
  • 46
  • 59
  • 2
    Related: http://stackoverflow.com/questions/7453269/class-with-virtual-functions-takes-more-place So the short answer is no. – Mysticial Feb 28 '12 at 06:03
  • The standard does not specify this. – Martin York Feb 28 '12 at 06:39
  • @LokiAstari: No, but the standard does specify which classes are polymorphic, and in a sensible implementation those will be the only ones with a vtable or equivalent. – Mike Seymour Feb 28 '12 at 07:15
  • @MikeSeymour: Yes me pedantic. But what I am trying to say that the standard does not specify how polymorphism effects the layout of the class (if at all). – Martin York Feb 28 '12 at 07:17
  • Possible duplicate of [Do all classes have a Vtable created for them by the compiler?](https://stackoverflow.com/questions/5709892/do-all-classes-have-a-vtable-created-for-them-by-the-compiler) – Bergi Aug 29 '17 at 05:55

4 Answers4

6

The language specification of C++ does not define what a "vtable" is, or which classes need one.

A particular implementation of C++ in a compiler often uses a vtable to implement virtual methods. If a class has no virtual methods (and no superclasses with virtual methods), then the compiler may omit the vtable. However, keep in mind this is purely a compiler implementation decision, and not required by the standard.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • 2
    Clarification, how it implements virtual is implementation defined, however, the rules on standard layout types are pretty strict and the compiler has little freedom. That is, a standard layout type cannot contain a vtable, it would break the rules on standard layout. – edA-qa mort-ora-y Feb 28 '12 at 07:33
  • @edA-qamort-ora-y: I'm not sure if that's correct. Obviously you can't have a `vptr` as the first member (first declared member must be at offset 0) but that's the only issue I see. – MSalters Feb 28 '12 at 08:32
  • I don't believe you can have any special data. Consider that `memcpy` between layout-compatible types is allowed, and also via an intermediate character buffer. If this additional value identified the class somehow then copying it via memcpy would basically corrupt the target type, since its special value would not match what is expected of the class. If absolutely nothing used this value, that would be okay, but this would be the exact same as unnamed padding then. – edA-qa mort-ora-y Feb 28 '12 at 08:47
  • so do you mean there are better ways to do it ? – jokoon Feb 02 '13 at 12:36
3

As a non-standard rule of thumb (vtables are not dictated by the standard) which applies to virtually all compilers:

Only classes with virtual member functions and/or a virtual destructor have a vtable. Other classes not. This conforms to the general rule in C++ "pay for what you use".

Of course this puts you into an important responsibility: Is your class to be deleted polymorphically? I.e., will it be used as a public base class and be deleted through it? Then make the destructor virtual.

Sebastian Mach
  • 38,570
  • 8
  • 95
  • 130
2

C++ language as such doesn't talk about how virtual functions needs to be implemented i.e. it can using vtables or any other mechanism. Having said that, generally it is implemented using v-table, and this v-table is created only if the class contains virtual functions.

Naveen
  • 74,600
  • 47
  • 176
  • 233
1

v-table holds the function's address in it. This table will hold the function address of all the virtual functions that are defined in the base class. Based on the actual object type, this address changes and the exact function is called.

If the class does not inherits any of the class with virtual function, it need not hold any v-table. All the functions calls will be linked compile time.

Rajan
  • 292
  • 2
  • 10