collection, aggregate and composition

Discussion in 'C++' started by Jarek Blakarz, Nov 12, 2012.

  1. Hi

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

    thanks in advance
    Jarek Blakarz, Nov 12, 2012
    #1
    1. Advertising

  2. Jarek Blakarz

    Rui Maciel Guest

    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
    Rui Maciel, Nov 12, 2012
    #2
    1. Advertising

  3. 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 ?
    Jarek Blakarz, Nov 13, 2012
    #3
  4. Jarek Blakarz

    Rui Maciel Guest

    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
    Rui Maciel, Nov 13, 2012
    #4
  5. Jarek Blakarz

    ralph Guest

    On Tue, 13 Nov 2012 02:40:29 -0800 (PST), Jarek Blakarz
    <> 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
    ralph, Nov 13, 2012
    #5
  6. Jarek Blakarz

    Öö Tiib Guest

    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.
    Öö Tiib, Nov 13, 2012
    #6
  7. 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
    Saeed Amrollahi, Nov 14, 2012
    #7
  8. Jarek Blakarz

    Jorgen Grahn Guest

    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 .
    Jorgen Grahn, Nov 15, 2012
    #8
  9. Jarek Blakarz

    Rui Maciel Guest

    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
    Rui Maciel, Nov 15, 2012
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rickman
    Replies:
    3
    Views:
    4,954
    rickman
    Jul 15, 2003
  2. aggregate operator

    , Jul 11, 2005, in forum: VHDL
    Replies:
    3
    Views:
    1,333
  3. jens
    Replies:
    1
    Views:
    645
    Mike Treseler
    Oct 6, 2005
  4. Replies:
    1
    Views:
    537
    Jim Langston
    May 2, 2007
  5. Øyvind Isaksen
    Replies:
    1
    Views:
    927
    Øyvind Isaksen
    May 18, 2007
Loading...

Share This Page