Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   collection, aggregate and composition (http://www.velocityreviews.com/forums/t954432-collection-aggregate-and-composition.html)

Jarek Blakarz 11-12-2012 12:42 PM

collection, aggregate and composition
 
Hi

Could you please explain me the difference between collection, aggregate and
composition. For me all of them are the same.

thanks in advance

Rui Maciel 11-12-2012 02:25 PM

Re: collection, aggregate and composition
 
Jarek Blakarz wrote:

> Hi
>
> Could you please explain me the difference between collection, aggregate
> and composition. For me all of them are the same.


Collection refers to a set of data types, which don't necessarily have a
relationship among them or with the collection data type. An example would
be a container data type.

The meaning of the terms "aggregate" and "composition" depend on the
context. When used to express associations between classes, their meaning
differs essentially on how the life of the associated objects are managed.
More specifically, if an object of type A is composed by an object of type B
then if A's life ends then B's life will also end, whereas with an
association relationship A's life may end and B's life cycle won't be
affected by it.


Rui Maciel

Jarek Blakarz 11-13-2012 10:40 AM

Re: collection, aggregate and composition
 
On Monday, November 12, 2012 3:25:05 PM UTC+1, Rui Maciel wrote:
> Jarek Blakarz wrote:
>
>
>
> > Hi

>
> >

>
> > Could you please explain me the difference between collection, aggregate

>
> > and composition. For me all of them are the same.

>
>
>
> Collection refers to a set of data types, which don't necessarily have a
>
> relationship among them or with the collection data type. An example would
>
> be a container data type.


Is an ordinary C++ array "int elems[10]" a collection ?

Rui Maciel 11-13-2012 11:17 AM

Re: collection, aggregate and composition
 
Jarek Blakarz wrote:

> Is an ordinary C++ array "int elems[10]" a collection ?


Yes, it is. Or at least it is supposed to be. It appears that some people
argue that a fixed-size container is not a collection, and instead it is
supposed to be a composition. That would mean that data structures such as
tuples, arrays, fixed-size sets and lists, and even some aggregate types
wouldn't be collections. Nevertheless, the C++ standard defines tuples as
"heterogeneous, fixed-size collections of values", which backs up the
assertion that tuples, and by induction the other data types I've mentioned,
are collections.


Rui Maciel

ralph 11-13-2012 03:33 PM

Re: collection, aggregate and composition
 
On Tue, 13 Nov 2012 02:40:29 -0800 (PST), Jarek Blakarz
<jumianek@gmail.com> wrote:

>
>Is an ordinary C++ array "int elems[10]" a collection ?


As Rui points out, there are those that like more complex definitons,
but in general - a collection 'is a' bag.

No matter how complex the rules might be for placing items into or
removing, nor no matter how complex the construction or interfaces
might appear, if an object fits the simple analogy of a "bag" - it is
a collection. <bg>

-ralph

Öö Tiib 11-13-2012 08:02 PM

Re: collection, aggregate and composition
 
On Tuesday, 13 November 2012 12:40:29 UTC+2, Jarek Blakarz wrote:
> Is an ordinary C++ array "int elems[10]" a collection ?


"int elems[10]" is called C array or raw array. Equal weight C++ array
is "std::array<int,10> elems". C++ programmers usually name such things
with common term "containers".

Java programmers call similar things in their language "collections".
Probably they would also call a C array as "collection" if they
ever have to talk about one. So in that sense it is indeed a collection.

Saeed Amrollahi 11-14-2012 07:20 AM

Re: collection, aggregate and composition
 
On Monday, November 12, 2012 4:12:07 PM UTC+3:30, Jarek Blakarz wrote:
> 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

Jorgen Grahn 11-15-2012 08:52 PM

Re: collection, aggregate and composition
 
On Wed, 2012-11-14, Saeed Amrollahi wrote:
> On Monday, November 12, 2012 4:12:07 PM UTC+3:30, Jarek Blakarz wrote:
>> 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 priv[...]


I was going to complain, but I see it's in Stroustrup's glossary:
http://www.stroustrup.com/glossary.html

Still, I don't think I've ever heard it used like that. If it's a
term in the standard, I don't think it has caught on very well
outside it.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Rui Maciel 11-15-2012 09:06 PM

Re: collection, aggregate and composition
 
Jorgen Grahn wrote:

> I was going to complain, but I see it's in Stroustrup's glossary:
> http://www.stroustrup.com/glossary.html
>
> Still, I don't think I've ever heard it used like that. If it's a
> term in the standard, I don't think it has caught on very well
> outside it.


From ISO/IEC 14882:2011:

8.5.1 Aggregates
1 An aggregate is an array or a class (Clause 9) with no user-provided
constructors (12.1), no brace-or-equal-initializers for non-static data
members (9.2), no private or protected non-static data members (Clause 11),
no base classes (Clause 10), and no virtual functions (10.3).

The same text is included in ISO/IEC 14882:1998, without the brace-or-equal-
initializers bit.


Rui Maciel


All times are GMT. The time now is 10:58 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.