In what order are aggregated classes deleted?

Say I have a base class A that combines B and C:

class A  
{  
    B _b;  
    C _c;  
} 

in what order will _b and _c be deleted?
I read somewhere that this is the reverse order of their distribution.
So, I think in this little example, _c is deleted before _b, right?

Now, if I have a constructor A that looks like this:

A::A():  
_c(...),  
_b(...)  
{  
}

In what order are the _b and _c constructors called?
If the _b constructor is really called before _c alone (relative to their order in A), then I find it really counteracting intuitive!

In this case, what will be the order of destruction?

Thank you for your help!:)

(On a side note, I can't seem to type "}" in the Stackoverflow editor. Had to copy and paste from an external editor !?)

+3
3

( ) , . , , , . , , , , . , , .

, -, A. _b, _c, A. A , . . A , _c , _b ( ). , "A" .

+6

, .

, . , , .

_c , _b , .

+7

, -.

in your case

A::A():  
_c(...),  
_b(...)  
{  
}

implies the same build / destroy order as

A::A():  
_b(...),
_c(...)  
{  
}
0
source

Source: https://habr.com/ru/post/1722483/


All Articles