Original Post
Hi. I have a structure that represents a node in a linked list:
Node *n = new Node;
Now the space has been allocated on the heap.The name(string name) of the Foo object is allocated on the heap. Even if n was created on the stack, Foo::name would be still allocated on the heap.
But what is the case of Foo::age when n is allocated on the heap? Is it allocated on the heap or on the stack?
template <class T>
struct Foo
{
public:
Foo()
{
name = new string;
age = 10;
}
~Foo()
{
delete name;
}
private:
Foo(const Foo&);
Foo &operator = (const Foo &);
string *name;
int age;
};
struct Node
{
T data;
Node<T> *next;
};Node
Now the space has been allocated on the heap.The name(string name) of the Foo object is allocated on the heap. Even if n was created on the stack, Foo::name would be still allocated on the heap.
But what is the case of Foo::age when n is allocated on the heap? Is it allocated on the heap or on the stack?