C++ for Embedded · Interview question

Is overloading the same as polymorphism?

A strong answer

No, they operate at different times on different information. Overloading is static (compile-time) polymorphism: the compiler picks among overloads based on the static types of the arguments at the call site, and emits a direct call with zero runtime cost. Runtime polymorphism, virtual functions, dispatches based on the dynamic (actual) type of an object at run time, through a vtable, which costs an indirection and a per-object vtable pointer. So overloading can't choose an implementation based on what an object actually is at runtime; only virtual dispatch can. This distinction matters on embedded: overloading and templates are zero-overhead and used freely, while virtual functions add cost and are used more selectively.

What a weak answer sounds like

You know the answer. Do you know what gets you dinged?

Pro breaks down the answer most candidates actually give to this question — and the specific reason an interviewer marks it down. It’s the difference between sounding correct and sounding senior, on all 472 questions.

From the lesson

Function & Operator Overloading

One name, several type-specific implementations the compiler picks for you, and giving your own types natural operators, without the runtime cost of C's void* or tagged dispatch.

More Function & Operator Overloading questions

Browse all 472 interview questions
Is overloading the same as polymorphism? | EmbeddedPrep.io