rspec/rules/S3252/cfamily/rule.adoc

57 lines
1.2 KiB
Plaintext
Raw Normal View History

== 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.
2020-06-30 12:48:39 +02:00
=== Noncompliant code example
2020-06-30 12:48:39 +02:00
2022-02-04 17:28:24 +01:00
[source,cpp]
2020-06-30 12:48:39 +02:00
----
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
2020-06-30 12:48:39 +02:00
2022-02-04 17:28:24 +01:00
[source,cpp]
2020-06-30 12:48:39 +02:00
----
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::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::message.adoc[]
'''
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]