object containing another object of the same class
Folowing code is suposed to hold any amount of information under one name. Yet it doesn't work. Can anyone tell me why?
1 2 3 4 5 6
|
class A
{
public:
int x;
A next;
}list;
|
Because you could theoretically use up an infinite amount of memory like that, and C++ can't count to infinity.
if you wanna make an int list next must be a pointer to an A object, not directly an A object.
That way anytime your system allocate an A object it must allocate another A object, then another and so on till your system finishes his memory.
I think you can try the following way if you know pointers:
1 2 3 4 5
|
class Node {
public:
int data;
A * next;
};
|
Topic archived. No new replies allowed.