Senior
What are the disadvantages of virtual tables?
sobes.tech AI
Answer from AI
Virtual tables in C++ (and, consequently, in the implementation of virtuality in Android NDK):
- Performance: Slight overhead when calling a virtual function due to the need to look up the table and indirect call. This can be noticeable in performance-critical sections.
- Object size: Each object with virtual functions contains an additional pointer to its virtual table, increasing the object's size.
- Implementation complexity: Implementing virtuality at the compiler and runtime level adds an abstraction layer that may not be obvious to beginner developers.
- Inability to perform static analysis: Since the specific function called is determined only at runtime (by the pointer in the virtual table), compiler optimizers cannot fully analyze and possibly inline the virtual function call. This limits some optimizations.
- Serialization complexity: When serializing objects with virtual functions, it is necessary to correctly save and restore type information so that the virtual table pointer is properly initialized during deserialization.
- Limited access: Standard C++ does not provide a direct way to access or manipulate the virtual table of an object. This is a deliberate restriction to maintain safety and abstraction.
- Not applicable to certain types: Virtual functions cannot be applied to static functions, constructors, and non-static member functions that are not explicitly declared virtual.