Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Uncertainties about OOP and the struct Keyword

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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 class NodeType?

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).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading