HomebaseWorks

C++ interview questions

C++ questions that come up often in technical interviews, with answers that explain how the language actually behaves.

EWritten by the HomebaseWorks editorial team · reviewed 26 Oct 2026
1
What is the difference between a pointer and a reference?

A reference is an alias for an existing object: it must be initialised, cannot later refer to something else, and cannot be null in valid code. A pointer stores an address, can be null and can be reassigned. I use references for parameters that must refer to a real object, and pointers, preferably smart pointers, when something is optional or ownership matters.

2
How do virtual functions work?

When a class has virtual functions, compilers typically create a table of function pointers for it, and each object carries a hidden pointer to its class's table. A call through a base pointer looks up the function in that table at runtime, so the derived version runs. This is what lets code loop over a list of Shape pointers and still draw circles and squares correctly.

3
What is RAII and why does it matter?

RAII ties a resource to an object's lifetime: the constructor acquires it and the destructor releases it. Because destructors run automatically when a scope ends, including during exception unwinding, the resource cannot leak. std::vector, std::unique_ptr and std::lock_guard are everyday examples.

Advertisement
4
What is the difference between unique_ptr and shared_ptr?

unique_ptr has single ownership: it cannot be copied, only moved, and it deletes the object when it goes out of scope. shared_ptr allows shared ownership through a reference count and deletes the object when the last owner is gone. I default to unique_ptr and use shared_ptr only when ownership is genuinely shared, because the counting has a cost.

5
When would you choose map over unordered_map?

std::map keeps keys sorted and gives logarithmic lookup, which helps when I need ordered output or range queries. std::unordered_map uses hashing for average constant-time lookup, but its iteration order is unspecified. For a word count where order does not matter I would use unordered_map; to print the results alphabetically, map.

6
What is the rule of three?

If a class needs a custom destructor, copy constructor or copy assignment operator, it usually needs all three, because it is managing a resource by hand. Since C++11 this extends to the rule of five, adding the move constructor and move assignment operator. Better still is the rule of zero: hold resources in members such as vector or unique_ptr so the compiler-generated versions are correct.

Practise out loud. Say each answer once as written, then again in your own words with your own number or story.

Have a number ready. "My C++ score is at benchmark on a timed test" is a stronger answer than "I am good at it". Take the test and quote the result.

Advertisement
Continue with C++4 lessons · 1 interview set · 1 test · 1 certificate
Not tested

More in Programming