Pergunta

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?

Foi útil?

Solução

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.

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top