C++ for Embedded · Interview question

What is the this pointer?

A strong answer

this is an implicit pointer to the object a non-static method was invoked on, passed as a hidden first argument. Inside a method, an unqualified member name like base_ is shorthand for this->base_. It's what lets one shared copy of the method code operate on whichever object you called it on, a.send() and b.send() run the same code with this pointing at a or b respectively. In a const method, this is a pointer-to-const, so the compiler forbids modifying members. You rarely write this-> explicitly except to disambiguate a member from a parameter of the same name, or to return *this for chaining.

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

Classes & Encapsulation

Bundle data with the code that operates on it, and hide the internals behind a clean interface: the C struct-plus-functions pattern, made safe by the compiler.

More Classes & Encapsulation questions

Browse all 472 interview questions