← Sunrise Interview Insights

Sunrise·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Apr 2026

Summary

Technical phone screen for a Software Engineer role at Sunrise. Pretty C++ heavy, went deep on object model internals which I wasn't fully expecting.

Questions Asked (1)

Q1

Can a constructor be virtual in C++? Why or why not, and what's the standard workaround?

Technical Trade-offsSystem Design
Author's notes

I knew the answer but fumbled explaining the vtable part.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by directly answering 'no' and explaining the fundamental reason: constructors don't have a return type or vtable entry, and virtual dispatch requires an existing object. Then describe the standard workaround using a virtual factory method or the Factory pattern, and optionally mention the Named Constructor idiom.

Pro tip: Mention that while constructors can't be virtual, destructors should be virtual in polymorphic base classes to avoid undefined behavior—this shows you understand the broader object lifecycle and common pitfalls.

1. Direct Answer

State clearly that constructors cannot be virtual in C++. This sets a confident tone and avoids ambiguity.

2. Explain Why Not

Explain that virtual dispatch requires a fully constructed object with a vtable pointer, but during construction the object is incomplete and its dynamic type is not yet established. Also, constructors don't have a return type or a vtable slot.

3. Describe the Workaround

Introduce the Factory Method pattern: a virtual function that creates and returns a pointer to a new object. Alternatively, mention the Named Constructor idiom for controlled construction.

4. Provide a Code Example

Sketch a simple example: a base class with a virtual create() method, and derived classes overriding it to return their own type. This demonstrates practical understanding.

5. Discuss Trade-offs and Best Practices

Mention that virtual destructors are essential for polymorphic deletion, and that factory methods add a layer of indirection but enable flexible object creation.

Key Points to Mention

  • Constructors cannot be virtual because virtual dispatch requires an existing object with a vtable pointer.
  • During construction, the dynamic type of the object is the type of the constructor being executed, not the most-derived type.
  • The Factory Method pattern (virtual function returning a new object) is the standard workaround.
  • The Named Constructor idiom can also be used for controlled construction.
  • Virtual destructors are crucial in polymorphic base classes to ensure proper cleanup.
  • C++ object construction order: base class constructors run before derived class constructors, so virtual calls in constructors resolve to the current class's override, not the derived one.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.