0

Shouldn't members that are initialized in-class be composite members? Why does it matter to anyone that C++11 dictated that members that are initialized in-class cannot be aggregate members? I cannot think of a situation in which it would even be possible to initialize a proper aggregation member in-class. For context, the final paragraph of this answer is what sparked this question, and below are notes that outline my understanding of compositions vs. aggregations.

-> Composition vs. Aggregation and In-class vs. Initializer list
                                   member initialization
    Composition and aggregation members:
    - Are part of the object.
    - Do NOT know about the existence of the object.
    Composition members:
    - Can only belong to one object.
    - Existence is managed by the object to which it belongs.
    Aggregation members:
    - Can belong to multiple objects.
    - Existence is NOT managed by the object to which it belongs.
    
    In-class and initializer list member initialization:
    - Can initialize const members.
    In-Class member initialization:
    - Done in the header file where the class is defined.
    Initializer list member initialization:
    - Done before the body of a constructor for the class.
    
    As this topic pertains to C++:
    - Prefer composition (more consistent clean-up).
    - Composition members are cleaned up by the object to which they belong.
    - Aggregation members are NOT cleaned up by the object to which they belong.
    - C++11 dictates that in-class member initializers preclude the member from
      being part of an aggregation.
mcmuffin6o
  • 348
  • 1
  • 9
  • If a class needs to initialise an object it does not own then (a) who does own the object and (b) why didn't the owner initialise it ? – Richard Critten Jun 29 '22 at 20:51
  • @RichardCritten - My question exactly. – mcmuffin6o Jun 29 '22 at 21:01
  • The problem is the definition of Aggregate above does not match the C++ definition of Aggregate [dcl.init.aggr](https://eel.is/c++draft/dcl.init.aggr#:aggregate) C++ Aggregates are owned by any enclosing class. So we have a confusion of precise C++ definition and loose OOP definition and therefore the question is either unclear or impossible to answer as asked. – Richard Critten Jun 29 '22 at 21:05
  • @RichardCritten - Does my updated title make the question possible to answer? In case you didn't realize, a possible answer is: "it is not possible", followed by an attempt at explaining why people got upset about the C++11 standard being written the way it was. – mcmuffin6o Jun 29 '22 at 21:19
  • I will admit, this is sort of a worthless question, and only something that's bugging me without a definitive answer. Perhaps I need to take another look at how it's posed. – mcmuffin6o Jun 29 '22 at 21:20
  • This line _"Aggregation members: ... Existence is NOT managed by the object to which it belongs..."_ directly contradicts the C++ Standard. So do want an answers based on OOP definitions described in the question or C++ Standard definitions ? – Richard Critten Jun 29 '22 at 21:20
  • @RichardCritten Aha! So it was a misunderstanding on my part. Go ahead and answer this question with that and I'll mark it as accepted. – mcmuffin6o Jun 29 '22 at 21:22

0 Answers0