1

Wouldn't it be the same to just have an embedded object of the same structure that is not a pointer?

Struct Node
{
int data;
Node next;
};
//vs
Struct Node
{
int data;
Node * next;
};
3
  • Do you come to C++ from Python or Java? Commented Mar 6, 2016 at 2:48
  • 3
    If you have a house, can you embed another similar house inside that house ? No. But you can leave a note in that house with the address of another house. Commented Mar 6, 2016 at 2:52
  • @nos: nice example I like it Commented Mar 6, 2016 at 3:17

1 Answer 1

1

No!

Having the following struct:

struct Node {
    Node other;
};

Is illegal! Node doesn't have a defined size; the compiler can't build it correctly. A Node would contain a Node which would contain a Node.. wait what?

A pointer however is fine, it just points to a section of memory. When defining a pointer, the type that it points to doesn't have to be complete, it just has to defined.

struct Node;

int main() {
    Node* a; // Fine, no errors.
    Node b; // Incomplete type error
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.