Hi
Could you please explain me the difference between collection, aggregate and
composition. For me all of them are the same.
thanks in advance
Hi
In addition to other mentioned, you should consider
in which context we are talking. In standard C++, the
term 'aggregate' is used for arrays and classes without
user-defined constructors and class related facilities like private or protected members, or base classes and so on. Roughly, it means C-style structswith implicitly declared special member functions. Based on this definition, standard array container is an aggregate too.
The words collection and composition aren't standard C++ terms. I searched the whole last C++ standard draft, collection is used as a general word andcomposition (a couple of times) is used for function object composition.
In the context of general object-oriented programming
In general object-oriented context or UML, there is a class relationship called 'Has a' or 'whole/part'.
Composition is a strong and Aggregate is a week kind of
such relationship. Consider the following piece of code
class Point {
int x, y;
public:
Point() : x{0}, y{0} {}
};
class Circle1 {
Point center; // composition
public:
Circle1() : center{} {}
} C1;
Point Cent;
class Circle2 {
Point* center; // aggregate
public:
Circle2() : center{&Cent} {}
} C2;
In C1, by constructing object, center is
created and by calling destructor, it's center
is destructed. But in C2, after destruction,
Cent is present.
Java/C# use the word collection like container in C++.
HTH,
-- Saeed Amrollahi Boyouki