Customize function order in virtual method table?

  • How can I control the order of virtual functions in a virtual table? Are they laid out in the same order in which they are announced?

  • When a class with a virtual table is inherited, the virtual table of the inherited class is an extension of the base class or represents a completely new virtual table created only with virtual functions of the inherited classes. (i.e. virtual table still has class index + 0x0?)

+3
source share
5 answers
  • (a) Regarding the standard, you cannot (in fact, you cannot even assume that vtables exist). (b) It is possible, but what are the circumstances in which you need to control the order, but you cannot verify yourself? The way to check is to look at the disassembly of the virtual call (and find the offset (s) added to the vtable pointer to get the address of the call) or look at the disassembly of the vtable itself.

  • Depending. For single inheritance, perhaps this is an extension of the base class, and index 0 of each object points to a virtual table for the class with a pointer to the correct implementation (possibly overriding) for each virtual function declared in the base classes, and then pointers to each virtual function, declared in a derived class. For multiple and virtual inheritance, this is not (cannot be) simple. Each object will contain several pointers, either to vtables or to structures that contain vtables plus other class information, and when you throw around the class hierarchy, the value of the object pointer changes. Try and see.

All this for a very hypothetical "typical implementation." Compiler compilers have their own tricks.

+6
source

This is a complete implementation. The C ++ standard does not specify any table of virtual functions at all - this is how it is usually implemented.

+7
source

A virtual table is implementation specific. It can be laid out in any order. There can even be no virtual table for implementing polymorphism. I recommend this Wikipedia article that answers your questions.

+2
source

Although, of course, 100% true, other answers ignore the obvious truth.

This is not a silly question, depending on the purpose of the posters. Too often, we are forced to use platform technology to achieve our goal. In particular, what the poster is trying to do is strikingly similar to COM on windows ... writing clean virtual abstract interfaces that can be obtained from one of the few ways to get bulletproof C ++ libraries without dropping back to the C interface.

I ran into the same problem when writing a plugin architecture in my native C ++ - the lack of an ABI means that it is incredibly difficult to interact with.

+1
source

The question that your question really raises is, for what reason would you ever need to have a virtual table in a specific order?

One of the reasons this depends on the implementation is because the compiler can make choices about how to build the vtable for performance reasons or other requirements, especially for a given CPU architecture.

0
source

Source: https://habr.com/ru/post/1241431/


All Articles