Why is this an issue?
In the interest of code clarity, static
member variables of a base class should never be accessed using a derived type’s name. Doing
so is confusing and could create the illusion that two different static variables exist. If the variable is const
, there is no risk of
confusion.
Noncompliant code example
class Parent {
public:
static int count;
static Color const defaultColor = green;
};
class Child : public Parent {
public:
Child() : myColor(Child::defaultColor) // Compliant, this is a constant
{
Child::count++; // Noncompliant
}
};
Compliant solution
class Parent {
public:
static int count;
static Color const defaultColor = green;
};
class Child : public Parent {
public:
Child() : myColor(Child::defaultColor) // Compliant, this is a constant
{
Parent::count++;
}
};