0

I understand that objects initialized with 'new' are allocated from the heap, but what about their members? For example, I have class A:

class A
{
        private: int a;   //here "a" should be on stack  
};

Then I have object A defined in following code respectively

A a;

A *ap = new A();

Now the first statement places a on stack and ap will be in the heap, but how about a.a and ap->a? Are they with their parent objects?

4

2 に答える 2

1

はい、定義上、オブジェクトがヒープ上にある場合、そのメンバーも同様です。スタックも同様です。

オブジェクトは実際には単なるメモリの塊です。オブジェクトにはメンバーが含まれているため、メンバーのメモリはそこにあります。

于 2012-09-12T03:18:32.017 に答える
0

The members are part of the object, just like with a struct.

于 2012-09-12T03:17:10.767 に答える