You will always need a pointer (or a reference) when implementing a linked list. This is needed in order to link to the next value in the list. For example, for your code you will need
class Sample{
public:
Sample *next;
void someFunction();
};
You cannot build something like this:
class Sample{
public:
Sample next;
void someFunction();
};
because there is will be infinite samples boxed in one another. The compiler cannot know how long the list should be (tho you can use some template arguments for this if you know the size at compile time)
And next will point to the next value in the list. So in your main you will have:
int main() {
// allocate 2 Sample objects
Sample *s1 = new Sample();
Sample *s2 = new Sample():
// link them together
s1->next = s2;
delete s1;
delete s2;
}
Of course, you can have them local to main for example:
int main() {
// 2 Sample objects on the stack
Sample s1;
Sample s2:
// link them together
s1.next = &s2;
}
the big difference here is that s1 and s2 are local to the main function. This is fine as long as we talk about main, but you cant do something like this:
Sample f() {
Sample s1, s2;
s1.next = &s2;
return s1;
}
because s2 does not live after f ends, so s1.next will be an invalid pointer.
Sampleinside aSample(inside aSample...), since the universe would run out of space before you had a single one.