rspec/rules/S3229/cfamily/rule.adoc

44 lines
1.2 KiB
Plaintext
Raw Normal View History

2021-04-28 16:49:39 +02:00
Class members are initialized in the order in which they are declared in the class, not the order in which they appear in the class initializer list. To avoid errors caused by order-dependent initialization, the order of members in the initialization list should match the order in which members are declared in a class.
2021-04-28 16:49:39 +02:00
== Noncompliant Code Example
----
#include <iostream>
class C {
public:
int x;
int y;
C(int i) : y(i), x(y + 1) { } // Noncompliant
};
int main() {
C c(1);
std::cout << c.x << " " << c.y << std::endl; // prints 1 1
}
----
2021-04-28 16:49:39 +02:00
== See
* https://wiki.sei.cmu.edu/confluence/x/dXw-BQ[CERT, OOP53-CPP.] - Write constructor member initializers in the canonical order
* https://github.com/isocpp/CppCoreGuidelines/blob/036324/CppCoreGuidelines.md#c47-define-and-initialize-member-variables-in-the-order-of-member-declaration[{cpp} Core Guidelines C.47] - Define and initialize member variables in the order of member declaration
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[]