rspec/rules/S3252/cfamily/rule.adoc

45 lines
1022 B
Plaintext
Raw Normal View History

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.
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++;
}
};
----
ifdef::rspecator-view[]
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::rspecator-view[]