Are the members of a heap allocated class automatically allocated in the heap?

前端 未结 2 950
伪装坚强ぢ
伪装坚强ぢ 2021-01-20 04:25

Let\'s say I have:

class A{
    public:
    int x;
    int y;
};

And I allocate an A instance like:

A *a = new A();
         


        
2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-20 04:52

    You will get one allocation on the heap that contains x and y. A is just a nice way to refer to that object.

    I wish I could easily draw on Stack Overflow, but you can think of it something like this (very small) heap example:

            +-------------------------------+
            |            Heap               | 
            +-------------------------------+
            | Other memory here ...         | 
            +-------+-----------------------+
    A *a -> | x | y | Other memory here ... |
            +-------+-----------------------+
    

提交回复
热议问题