class NodeType {
public:
int info;
NodeType* link;
};
I came across this when learning about linked list, and as a beginner, at line 4, pointer link
is an object of class NodeType
, this interpretation is definitely wrong, so can somebody please explain what does this line mean? I don't recall learning this when I am interacting with the concept of OOP.
struct NodeType
{
int info;
struct NodeType* link;
};
I take that this structure declaration here is of the same as the class declared above, so my second question is, why is there a second struct
keyword at line 4
? Can the keyword be removed? Is this the phenomenon called nested struct?
CodePudding user response:
Yes, the two snippets are the same.
why is there a second struct keyword at line 4?
It's called an elaborated type specifier (a type with struct
prepended to it, or class
/union
/enum
; the definition class NodeType {}
doesn't count as one).
It's useless here and can be removed. It's only useful when a struct is mentioned for the first time, so the compiler doesn't know it's a struct yet.
In this regard C is different from C, where you must prepend struct
every time to refer to a struct.
[is] pointer
link
is an object of classNodeType
?
No, an object of class NodeType
would be NodeType link;
, but then it wouldn't be a pointer.
You could say that link
is an object of type NodeType *
(a pointer to NodeType
).