Understanding object size and virtual methods in C++
This is an easy C++ language-knowledge question that tests your understanding of how the compiler lays out objects in memory, particularly when virtual methods are involved. It's the kind of detail-oriented question quant shops ask to verify that candidates have a solid grasp of C++ internals rather than just surface-level syntax.
The core concept is that adding even a single virtual method to a class changes its memory footprint in a specific, predictable way. On a 64-bit architecture, this has immediate and measurable consequences for object size. To answer correctly, you need to know how the compiler handles virtual function dispatch and what data it must store alongside your instance variables.
- Virtual method tables (vtables) and pointers
- Object memory layout and alignment
- The sizeof operator and its interaction with virtual functions