What are aggregate classes for?
I just read the following definition in C ++ Primer (5th edition) on page 298:
A class is an aggregate if:
All of its data members are publicly available.
It doesn't define constructors
It has no initializers in the class
It has no base classes or virtual functions
This post also contains a definition: What are aggregates and PODs and how / why are they special? ...
After reading the previous sections of the book about the value of encapsulation, I'm wondering: why would anyone want to use an aggregate class? (By the way, the question seems to apply to struct
in general: why would I have a default public:
?)
source to share
An aggregate is basically a simple dataset that has no invariants to guarantee class
. Since there is no invariant, and therefore all combinations of possible member values make sense, there is no point in making them private, since there is nothing to protect.
A simple example for such a class would be something like
struct Point3d {
std::array<double, 3> coordinates;
};
Since each triple from double
is a point in R ^ 3, nothing happens by hiding the data. (If you are afraid of the values NaN
and infinity
, this may not be a good idea. If it does, you may not want to make it an aggregate.)
Another example of an aggregate type is std::array
. Again, this is just an array of some type and some (immutable) length, so no invariant should be protecting.
One of the advantages of aggregates is aggregate initialization .
source to share
Aggregate classes can be aggregate-initialized using an initializer list:
class Agg {
string a;
int b;
}
Agg a = {"A string", 0};
They are mainly used as tab return value as shown below:
class Response {
bool success;
string body; // Body is only set if success is true
}
Response foo() {
if(failed) return {false, ""};
return {true, "content"};
}
As you can see, all four requirements must be met for this to make sense (before C ++ 11).
All of its data members are publicly available.
It's pretty much just object overflow.
It doesn't define constructors
Well, either you initialize all members (via aggregate initialization), or you initialize all members by default (with a standard constructor).
It has no initializers in the class
Same as above
It has no base classes or virtual functions
It's like a tupple, so the base classes will destroy that and virtual functions don't make sense since you don't inherit from such a class.
Hope this clears up a bit. Remember this was invented before C ++ 11 where it std::tuple
didn't exist, so you would use an aggregate class to return multiple values. Nowadays, in most use cases you can use tuple
, but this is not my favorite.
std::tuple
against aggregate classes
- Aggregate class can define member functions
- An aggregate class can overload operators.
- Aggregate can define useful names for its attributes
- etc. to expand
In most cases, aggregate classes are the way to go. a tuple should only be used if you really don't want anything other than returning a tuple of objects and then "chopping it up". Member functions don't make sense most of the time, but aggregated classes can, for example, overload the translation operator.
Use tuples only when you just don't want to do anything other than reconfigure a bunch of objects without having to "name" them (via member names in aggregates)
source to share