What is the point of declaring a class variable in a class?

I see this sometimes and it confuses me as to why people are doing it?
1
2
3
4
5
6
7
8
struct things {
private:
    class something; //What is this doing here?
    int value;
    //etc
public:
    //the interface and all that
};


So yeah what is the purpose of class something? I'd understand if the friend keyword was there like this "friend class something;". But it isn't and so what exactly is this line supposed to be for?
It looks like a forward declaration to me but your example might be overly simplistic. If 'something' is defined in another file yet 'things' still has to know about it, then you have to tell the compiler to expect the definition to come later.
I think you are actually asking about nested classes. Let's say you have a LinkedList class which is a list of Node objects. Now let's say you want to write a BinaryTree class which is a tree of Node objects. However, a LinkedList Node is completely different from a BinaryTree Node. So, you can't merely write a single Node class and go on your way.

Instead, define a Node class nested within the LinkedList class and write another one nested within the BinaryTree class. The two classes (LinkedList::Node and BinaryTree::Node) are distinct, and there is no name conflict between the 2.
Topic archived. No new replies allowed.