rspec/rules/S3252/cfamily/rule.adoc

38 lines
889 B
Plaintext
Raw Normal View History

2021-01-27 13:42:22 +01:00
In the interest of code clarity, ``++stati{cpp}`` 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.
2020-06-30 12:48:39 +02:00
== 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++;
}
};
----