C++ for Embedded · Interview question

Does a const member function guarantee nothing it touches can change?

A strong answer

No, const on a method is shallow. It guarantees the method won't modify the object's own data members (the compiler makes this a pointer-to-const), but if a member is itself a pointer, the const method can still modify what that pointer points at, because the pointer value (the address) is what's const, not the pointee. So a const method on a class holding uint8_t* buf_ can do buf_[0] = 0; legally, it changed the buffer, not the pointer. If you want to prevent that, the member should be const uint8_t* or you design the interface accordingly. This mirrors the pointer/pointee distinction from the C lesson and is a real source of "I marked it const, why did my data change" confusion.

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

const Correctness & constexpr

const propagates read-only intent through your whole API; constexpr moves computation to compile time so lookup tables and config land in flash with zero runtime cost.

More const Correctness & constexpr questions

Browse all 472 interview questions
Does a const member function guarantee nothing it touches can change? | EmbeddedPrep.io