3

Does the C++ spec allow an instance of a non-virtual class to include memory for a vtable pointer? I am asking this, because a colleague said he once used a C++ compiler where the following happened:

  class MyClass
  {

     public:

     HeaderStruct header; //This had extra words
     BodyStruct     message_body;
  };

He then changed the code to this, which got rid of the extra words:

  struct MyClass
  {

     HeaderStruct header; //This did not have extra words
     BodyStruct     message_body;
  };

None of these types was virtual or derived from anything virtual. So the theory was that perhaps this particular compiler allocated memory for a vptr for the class instances but not for the struct instances. So I'm just trying to determine if such compiler behavior is precluded by the spec.

Thanks!

Ken

3
  • 1
    What's "extra words"? And where did the vfptr theory come from - did sizeof change? Apr 27, 2011 at 2:10
  • 1
    I obviously can't say for sure (after all, compilers have a wide latitude to add extra words), but I think your buddy is wrong. I don't believe he ever used a C++ compiler where that actually happened. But, to answer your literal question, yes, C++ allows compilers to include extra space in classes for whatever purpose they care to use.
    – Robᵩ
    Apr 27, 2011 at 2:30
  • There is no such thing as a "virtual class" in std C++. Maybe you meant polymorphic class.
    – curiousguy
    Nov 24, 2018 at 4:02

4 Answers 4

7

By standard 'struct' and 'class' are synonyms that affect only default access to bases and members in class definition.

Standard defines POD (plain old data). POD may not have user defined constructors, destructors, assignment operators, non-static reference members and anything virtual (also non-static members of it should not have such things). PODs have strict memory layout rules (for compatibility with C) and so the implementation can not add there any vtables or RTTI information or the like.

However the C++ compilers of old times did often deviate from standard and from each other quite a bit so your colleague might be right too.

1
  • Now called standard layout types in C++0x I believe (since it includes more than just PODs) and trivially copyable types also provides bit-pattern guarantees. Apr 27, 2011 at 7:50
3

I'm unable to find an online reference for it with a quick search, but I'm pretty sure a compiler is permitted to do ANYTHING with the layout of any class; in particular, in classes without any virtual methods, it's allowed to put a vftp or not, depending on how it's feeling that day, whether it's declared as class or struct (which are equivalent in C++ except for the default access specifier), the phase of the moon, or anything else. The only restriction that I'm aware of is that the top part of a derived object shall match the layout of its first (non-virtual) base class. And I'm not even sure it has to be the first one.

You should not at any time depend on a particular compiler's decision on the layout of an object. Many compilers put a vftp in all objects without exception, in order to provide run-time type information to debuggers, or just to make their own lives easier. Some don't. You have no reasonable way of knowing, except through the sizeof operator.

2

There's no such thing as a "virtual class". You might mean virtual inheritance, which involves using the virtual keyword before a class name, but the virtual-ness applies to the inheritance relationship, not the class itself. Or maybe you just mean a class that contains virtual functions.

At any rate, the C++ spec doesn't say anything about virtual tables at all; those are specific to an implementation. Typically a virtual table will only be added if the class contains virtual functions, has virtual bases, or inherits from other classes that do. But it would be perfectly valid for an implementation to put a virtual table in every instance of every class.

4
  • 1
    The address of an instance of a class that does not contain any inheritance or virtual methods has to point to the first member as well. This does not preclude the addition of a superfluous vptr, but it does preclude it from being at its logical location. For what that's with. Apr 27, 2011 at 3:11
  • @Dennis Zickefoose: True, but I really should've been more general in my statement. The compiler could append the full text of the King James Bible to every class instance and still be standard-compliant. :-)
    – Wyzard
    Apr 27, 2011 at 3:21
  • I'm going to create a fork of gcc that does just that. "make it fast, make it small, make it spiritual. Pick two." Apr 27, 2011 at 4:15
  • 1
    POD types now, and standard layout types in C++0x, have specific memory layout requirements. The compiler may not choose to put random stuff in there -- or more specifically it has certain rules to follow. Adding a VTable to a standard layout class/POD is not allowed. Apr 27, 2011 at 7:52
-1

The virtual modifier for a class is only used if/when you might inherit from the class more than once. In the I/O streams library, istream and ostream both inherit from ios_base, and iostream inherits both istream and ostream. The virtual modifier allows you to inherit twice without getting two copies of the base class members.

But any method can be virtual, even in a non-virtual class -- and therefore any class can have a vtable.

But the real answer to your question :) is that a class and struct are almost identical, except that in a class, members are private by default, whereas in a struct they're public by default.

3
  • This doesn't really appear to answer the question asked; you seem to be describing virtual inheritance.
    – Paul Z
    Apr 27, 2011 at 2:24
  • Hi. When you say that any method can be virtual, even in a non-virtual class", I'm not following that. I thought that if a class has a virtual method, then by definition, it is a virtual class. Note that by a "virtual class", I do not mean that it has virtual inheritance. I just mean that it or one of its base classes has at least one virtual method. So by non-virtual, I mean that it and its base classes have no virtual methods. Does the spec allow such a class to have a vtable pointer?
    – KenK
    Apr 27, 2011 at 2:28
  • I don't believe the vtable is actually required by the spec, however that seems to be how everyone does it. And I believe most implementations use a vtable whether you have virtual methods or not.
    – David
    Apr 27, 2011 at 13:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.