liveBook · Manning

This pointer

As explained in 100 C++ Mistakes and How to Avoid Them

The this pointer is the implicit pointer available in a non-static C++ member function that refers to the object on which the function was invoked.

In a non-static C++ member function, the this pointer refers to the object on which the function was invoked. It becomes essential when a parameter shadows an instance variable: without it, an assignment can silently assign the parameter to itself. section 9.1

Mistake: assigning the parameter to itself

In setRadius(double radius), writing radius = radius leaves the object unchanged. Some compilers accept this code without warning, creating a difficult debugging problem. Write this->radius = radius instead; the receiver member is then visibly distinct from the parameter. The same naming pattern makes constructor initializer lists readable and documents which value initializes the instance variable. section 9.1

Listing 9.1 Identical variable names in various scopes
double rad = 1.0;                                            #1
class Circle {
private:
    double rad;                                              #2
public:
    Circle(double rad) : rad(rad) {}                         #3
    void setRadius(double rad) {                             #3
        if (rad < 0)
            throw std::invalid_argument("negative radius");
        rad = rad;
    }
    double sum() const {
        return rad + rad;                                    #4
    }
    double getRadius() { return rad; }                       #5
};
 
int main() {
    Circle c(3);
    std::cout << "radius is " << c.getRadius() << '\n';
    std::cout << "enlarged radius is " << c.sum() << '\n';
    return 0;
}

Inspect the Circle example where identical names across global, member, and parameter scopes make setRadius fail. section 9.1

Listing 9.3 Identical variable names distinguished by a namespace
namespace global {                                            #1
    double radius = 1.0;
};
class Circle {
private:
    double radius;
public:
    Circle(double radius) : radius(radius) {}
    void setRadius(double radius) {
        if (radius < 0)
            throw std::invalid_argument("negative radius");
        this->radius = radius;                                #2
    }
    double sum() const {
        return radius + global::radius;                       #3
    }
    double getRadius() const { return radius; }
};
 
int main() {
    Circle c(3);
    std::cout << "radius is " << c.getRadius() << '\n';
    std::cout << "enlarged radius is " << c.sum() << '\n';
    return 0;
}

Compare the corrected Circle implementation, which uses this->radius for assignment and a namespace qualifier for the global radius. section 9.1

How 100 C++ Mistakes and How to Avoid Them covers it

How deep it goes
Explains silent self-assignment and readable corrections, but stops short of broader pointer semantics.
Examples
Uses Circle radius assignments, constructor initialization, global variables, and namespace-qualified names.
What it assumes
Assumes basic C++ classes, member variables, parameters, constructors, and scope distinctions.
What you can do after
Diagnose and correct shadowed assignments by explicitly naming the receiving object member.
How it connects
Connects the this pointer with variable shadowing, member functions, constructors, initializer lists, mutators, and namespaces.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 C++ Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.