67 lines
1.3 KiB
Plaintext
67 lines
1.3 KiB
Plaintext
== Why is this an issue?
|
|
|
|
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
|
|
|
|
[source,cpp]
|
|
----
|
|
class A { // Noncompliant, A is an aggregate
|
|
public:
|
|
int x;
|
|
int y;
|
|
};
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,cpp]
|
|
----
|
|
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
|
|
|
|
* {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[]
|