Will heap allocated objects have their members allocated on the stack?

后端 未结 4 436
醉梦人生
醉梦人生 2021-01-22 17:15

Let\'s consider this example.

class StaticlyManagedObject
{
   //some class members....
}
class DynamiclyManagedObject
{
   StaticlyManagedObject _staticlyManage         


        
4条回答
  •  心在旅途
    2021-01-22 18:04

    A subobject has the same storage duration as the complete object it is a part of. If an instance of DynamiclyManagedObject is dynamically allocated, then the StaticlyManagedObject member will be destroyed when the DynamiclyManagedObject is destroyed.

    Informally, you might say that the subobject will be on the heap if and only if the complete object is on the heap. However, storage duration is the technically correct way to talk about it; heap and stack are implementation details.

提交回复
热议问题