C++ Language Internals: vtables, Multiple Inheritance, Copy Semantics, Smart Pointers
Company: Autodesk
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: easy
Interview Round: Technical Screen
A first technical round for a senior C++ role runs through a set of language questions. Answer each one the way you would for an interviewer who wants to hear what the compiler and the runtime do underneath, with a short code example where it helps.
### Clarifying Questions
- Which C++ standard does the codebase target? Thread-safe initialization of function-local statics, move semantics and `std::make_unique` depend on it.
- Should the answers describe a specific compiler ABI (the Itanium ABI used by GCC and Clang, or MSVC), or is the common implementation enough?
- Are these classes used across shared-library (DLL) boundaries? That affects the singleton answer.
### Part 1: Virtual functions, vtables and vpointers
What is a virtual function, and how does a call through a base-class pointer reach the derived override? Explain what the vtable and the vpointer are, where each one lives, when the vpointer is set during construction, and what a virtual call made from a constructor or destructor does. Why does a base class whose objects are deleted through a base pointer need a virtual destructor?
```hint Follow one call
Write `shape->area()` as the machine runs it: which loads happen before the jump into the function body, and where does each loaded value come from?
```
#### What This Part Should Cover
- The per-class table, the per-object pointer and the indirect call they produce.
- How the vpointer changes during construction and destruction, and what that means for virtual calls made there.
- The virtual destructor rule and the costs of virtual dispatch.
### Part 2: Multiple inheritance
How is an object laid out in memory when its class has two polymorphic base classes? What happens to the pointer value when a `Derived*` is converted to a pointer to its second base, and how does a virtual call through that base pointer get the right `this`? Describe the diamond problem and how virtual inheritance resolves it, including what virtual inheritance costs.
```hint Draw the bytes
Sketch an object of `D` where `struct D : A, B` and both `A` and `B` have virtual functions and data members. Mark the offset where the `B` part begins.
```
#### What This Part Should Cover
- Layout with one base subobject and vpointer per polymorphic base, and pointer adjustment on conversion.
- How an override reached through a non-primary base receives the correct `this`.
- The diamond (duplicated state, ambiguity) and the mechanics and cost of virtual inheritance.
### Part 3: The protected specifier and friend functions
What does the `protected` access specifier allow and forbid, including access to a protected member through an object of a different derived class? What is a friend function, what does declaring one do to encapsulation, and when is it the right tool?
```hint Try two siblings
Derive two classes from the same base and have a member function of one try to read the other's protected member.
```
#### What This Part Should Cover
- The exact protected access rule and the public, protected and private inheritance modes.
- What friendship grants and its limits: who grants it, whether it is inherited, whether it is transitive.
- Legitimate uses of friend and the encapsulation trade-off.
### Part 4: Deep versus shallow copy
A class owns a heap buffer through a raw pointer and has a destructor that frees it. What goes wrong with the compiler-generated copy constructor and copy assignment? Write a version that copies deeply, and explain the rule of three, the rule of five and where move operations fit. When is a shallow copy the right choice?
```hint Count the frees
Copy one object into another, let both go out of scope, and count how many times the same buffer is freed.
```
#### What This Part Should Cover
- What memberwise copy does to an owning pointer and the bugs that follow.
- A correct copy constructor and copy assignment, including self-assignment and exception safety.
- The rules of three, five and zero, and move semantics.
- Cases where sharing or viewing data without copying is correct.
### Part 5: Smart pointers
Compare `std::unique_ptr`, `std::shared_ptr` and `std::weak_ptr`: the ownership each one expresses and what each one costs. How does `shared_ptr` reference counting work, what does `std::make_shared` change, and which operations are thread-safe? How can reference cycles leak memory, and how do you prevent that?
```hint Where the count lives
Ask where a `shared_ptr` keeps its reference count and when that storage itself is freed.
```
#### What This Part Should Cover
- Ownership semantics and when to choose each pointer type.
- The control block, atomic counting and the allocation behavior of `make_shared`.
- Cycles, `weak_ptr`, and common misuse such as two owners created from one raw pointer.
### Part 6: Singleton design pattern
Implement a thread-safe singleton in C++. Explain why your version is safe when two threads request the instance for the first time at once, how and when the instance is destroyed, and the main drawbacks of the pattern.
```hint Use what the language guarantees
Compare a heap instance created under a lock with what the standard already promises about initializing static objects.
```
#### What This Part Should Cover
- A correct implementation and the guarantee that makes it thread-safe.
- Preventing copies and handling destruction at program exit.
- Drawbacks (hidden dependencies, testing, initialization and destruction order) and alternatives.
### What a Strong Answer Covers
- Explanations grounded in what the compiler emits: object layout, generated special member functions and indirect calls.
- Correct use of the C++11 and later guarantees.
- The memory and time cost of each feature.
- Judgment about when to avoid a feature, such as friend, singletons or inheriting implementation from several bases.
### Follow-up Questions
- How does `dynamic_cast` use the information stored alongside the vtable, and what does a failed cast cost?
- A `std::shared_ptr<Base>` constructed from `new Derived` destroys the object correctly even when `Base` has no virtual destructor, but a `std::unique_ptr<Base>` owning the same object does not. Why?
- How can a member function safely return a `shared_ptr` to its own object?
Overview: Senior C++ interview questions on the object model: virtual dispatch through vtables and vpointers, object layout under multiple inheritance, protected access and friend functions, deep versus shallow copy, smart pointers and a thread-safe singleton. It tests whether you can explain what the compiler generates and what each feature costs.
Read the full Autodesk Software Engineer interview experience this question came from