c++ - What are aggregate classes for? -


i have read following definition in c++ primer (5th edition) on page 298:

a class aggregate if:

  • all of data members public

  • it not define constructors

  • it has no in-class initializers

  • it has no base classes or virtual functions

a definition provided in post: what aggregates , pods , how/why special?.

after read in previous sections of book on value of encapsulation, wondering: why want use aggregate class? (btw, question seems applicable struct in general: why want have public: default?)

an aggregate simple collection of data not have invariants class have guarantee. since there no invariant , combinations of possible values of member make sense, there no point in making them private since there nothing protect.

a simple example such class like

struct point3d {    std::array<double, 3> coordinates; }; 

since every triple of doubles point in r^3, there nothing gain hiding data. (if afraid of nan , infinity values, might not best idea. if can happen, might want not make aggregate.)

another example of aggregate type std::array. again, array of type , (immutable) length, no invariant protect.

one advantage of aggregates aggregate initialization.


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -