Question

class OtherClass
{
    ...
};

class Test
{
    OtherClass otherClass;
};

int main()
{
    Test *pTest = new Test;
}

pTest points to an object of type Test. Is otherClass allocated on heap aswell or in the stack?

Était-ce utile?

La solution

Test *pTest = new Test;

Creates an Test object on Freestore(Heap), and all the members of Test are also on the same.
So Yes.

Note that technically, the standard never uses the terms Heap or stack but yes you can assume those because almost all implementations use them.

Autres conseils

It's allocated on the heap. Since pDataMember is a data member with type OtherClass (not a pointer!), it is part of the memory allocated for the object of class Test, hence it will be located wherever the Test object is located.

Maybe you should not name a non-pointer pDataMember? That's confusing.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top