Will the item be created on the stack?
Yes.
If so, what happens if MySecondClass is created via new ? Will this item still be on the stack?
Not. It will be stored along with the rest of the object, "on the heap" or wherever you are in a free store, or wherever the object is dynamically allocated (it could be some kind of memory pool or something else).
It is worth noting here that the terms "stack" and "heap" are usually used incorrectly. What you are really asking is the following:
Does member have auto-storage time? Yes.
Will it do this even if the encapsulation object has a dynamic storage duration? No β the dynamism of an encapsulating object is, in a sense, βinheritedβ.
[C++11: 3.7.5]: The storage duration of member subobjects, base class subobjects and array elements is their complete object (1.8).
The practical place in memory will in any case be the stack and free storage ("heap"), respectively, and this does not really matter.
And by the way, main should have an int return type.
Lightness races in orbit
source share