Thread: Nested Class Accessing Classes Private Variables

  1. #1
    Registered User
    Join Date
    Oct 2009

    Nested Class Accessing Classes Private Variables

    I have a Tree template -
    template<typename T>
    class Tree {
    	//node class
    	//nodes should have value, their parent, and all of their children
    	//and functions to get all of those
    	class Node {
    		//when declaring a node, give it
    		//a value and a parent value
    		Node(T&, Node*&);
    		bool isRoot();
    		T& getValue();
    		void setValue(T&);
    		Node*& getParent();
    		std::vector<Node*>& getChildren();
    		int getNumOfChildren();
    		std::string toString();
    		std::string childrenToString();
    		bool operator==(const Node& other);
    		T value;
    		Node* parent;
    		std::vector<Node*> children;
    	Node*& getRoot();
    	void add(T&, Node*&);
    	//root of tree
    	Node* dummyroot;
    	Node* root;

    I want the isRoot() function to just be something like this -

    template<typename T>
    bool Tree<T>::Node::isRoot() {return this == root;}

    When I do this, it tells me invalid use of nonstatic data member root. How can I access root without making it public? A couple random things I tried were-

    this == &root
    this == Tree<T>::root

    I don't want to give a Tree variable for each Node because I don't think it makes sense. I want the Nodes to just be a Node with a value, parent, and children. Any help would be appreciated.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    United States
    Since you are using a root variable without a particular association to a tree, you would get that error, since root is not a static member. I don't think making root a static member is the answer.

    Incidentally, isn't this something you'd want to handle in Tree? A tree ought to know where it's root node is. I would just make this a method for tree.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Currency Calculator
    By desiamerican in forum C# Programming
    Replies: 1
    Last Post: 01-21-2010, 09:53 PM
  2. accessing private class data low overhead
    By lawrenced in forum C++ Programming
    Replies: 23
    Last Post: 07-28-2009, 09:14 AM
  3. Replies: 10
    Last Post: 07-26-2008, 08:44 AM
  4. abstract class
    By xddxogm3 in forum C++ Programming
    Replies: 5
    Last Post: 01-01-2005, 09:08 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM