Christopher Pisz
Christopher Pisz

Reputation: 4010

Define Aggregate

Having a discussion with my lead pertaining to a code review and I was trying to explain my point of view, when I used the term "aggregate."

Evidently, we think that term is defined different ways.

I Googled it up, and all I can really find is: https://en.wikipedia.org/wiki/Object_composition#Aggregation

but that does not define what the aggregate is.

In the following code, which class is the aggregate and what would you call the other?

class A
{
};

class B
{
    std::vector<A> m_data;
};

I thought A is the "aggregate" He thinks B is the "aggregate"

I don't want to go through life misusing terms!

Upvotes: 0

Views: 280

Answers (2)

aschepler
aschepler

Reputation: 72473

In object oriented parlance, the aggregate is the composite object which contains the parts.

For example, https://www.uml-diagrams.org/aggregation.html uses the term in the sentence:

Example below shows Triangle as an aggregate of exactly three line segments (sides).

So for your example, B can be described as an aggregate of A objects, and its A objects are the parts. (Though there might also be other A objects which are not parts at all.)

(As a number of comments and another answer note, the term "aggregate" also has a more specific technical meaning in C++, describing cases when a type can be considered a sequence of its subobjects, with implications for initializing an object of that type using {} braces.)

Upvotes: 1

R Sahu
R Sahu

Reputation: 206727

According to the C++11 Standard:

An aggregate is an array or a class (Clause [class]) with no user-provided constructors ([class.ctor]), no brace-or-equal-initializers for non-static data members ([class.mem]), no private or protected non-static data members (Clause [class.access]), no base classes (Clause [class.derived]), and no virtual functions ([class.virtual]).

Given that, A is an aggregate type and B is not.
B can be one if you change it to:

class B
{
   public:
     std::vector<A> m_data;
};

Upvotes: 1

Related Questions