Reputation: 536
Iam having two classes Class A
and Class B
.
class A
{
int width;
int height;
};
class B
{
A obj;
};
i'm trying to create a pointer like below
B* myObj = new B();
Here, myObj
gets created in the heap. where does obj
, width
and height
gets created?
Upvotes: 3
Views: 208
Reputation: 62531
Objects can contain other objects, called subobjects. A subobject can be a member subobject, a base class subobject, or an array element.
The storage duration of subobjects and reference members is that of their complete object
The members of the B
object pointed to by myObj
are all contained within that B
object, and they all have the same (dynamic) storage duration.
Upvotes: 1
Reputation: 10489
The pointer to the object, called myObj
in your program, is created on the stack.
The object itself B()
is created on the heap. width
and height
are contained within the memory taken up by B()
and are thus also on the heap.
In Ascii Art:
Stack --- myObj
|
Heap [ B -- A [ Width, Height ] ]
Upvotes: 11