rspec/rules/S5558/cfamily/rule.adoc

67 lines
1.3 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
Aggregate classes are classes with no constructors and only public and non virtual base classes and members (the exact definition has changed with each version of the language, but the purpose is the same). Aggregates allow you to initialize a variable with lots of flexibility.
A negative aspect of this flexibility is that it is very easy for the user of an aggregate to leave some members uninitialized. Therefore, it's usually preferable to provide a set of constructors with your class to ensure the user will have to initialize it correctly.
This rule raises an issue when an aggregate has at least one non static data member with no in-class initializer.
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,cpp]
2021-04-28 16:49:39 +02:00
----
class A { // Noncompliant, A is an aggregate
public:
int x;
int y;
};
----
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,cpp]
2021-04-28 16:49:39 +02:00
----
class A {
public:
A(int x, int y) : x{x}, y{y}
{}
int x;
int y;
};
class A {
public:
int x=0;
int y=0;
};
----
== Resources
2021-04-28 16:49:39 +02:00
* {cpp} reference - https://en.cppreference.com/w/cpp/language/aggregate_initialization
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Add a constructor for this class
'''
== Comments And Links
(visible only on this page)
=== is related to: S5492
endif::env-github,rspecator-view[]