According to the C++ standard, this
can never be null, so comparisons of the two are pointless at best. At worst, because of compiler
optimizations, such comparisons could lead to null pointer dereferences or obscure, difficult-to-diagnose errors in production.
This rule raises an issue when this
is compared to nullptr
or 0 or anything #defined
as nullptr
or 0, such as NULL
in most environments.
Noncompliant code example
class MyClass {
string name;
string GetName() {
if (this != 0) { // Noncompliant
return name;
}
return 0;
}
}
Compliant solution
class MyClass {
string name;
string GetName() {
return name;
}
}