get method

This is a discussion on get method within the C++ Programming forums, part of the General Programming Boards category; Hi, in a header file I have a class called tree.h; inside that class I have a struct Code: struct ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    38

    get method

    Hi,

    in a header file

    I have a class called tree.h;

    inside that class I have a struct
    Code:
    struct tree_node
    	{
    		tree_node* left;
    		tree_node* right;
    		int x;
    		int y;
    	}; 
    	tree_node* root;
    inside the .cpp
    file i want to make a method called getRoot()?

    I have no idea how to do it

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,623
    When you say you want to make a method called getRoot() what do you want it to do? My guess is that it has to do with the root of the tree, but you don't need a function to "get" that. It's the first element of the tree. Use the root pointer.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    681

    Post

    I assume you pass node to a function that needs to obtain the top-most root of the tree.
    Also, I see a bit of a bad design here. Make a class TreeNode and add methods to it, like this:

    Code:
    class TreeNode {
    private:
        TreeNode* _parent;
        TreeNode* _left;
        TreeNode* _right;
    public:
        TreeNode* getRoot() const;
    }
    
    // in .cpp:
    
    TreeNode* TreeNode::getRoot() const
    {
        TreeNode* root = _parent;
        while (root)
        {
            root = root->_parent;
        }
        return root;
    }
    Would be nice to make _left and _right smart pointers.

  4. #4
    Registered User
    Join Date
    Jul 2009
    Posts
    38
    so the what i have it now, how can i pass in the root into the func without changing my code

  5. #5
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    265
    Quote Originally Posted by spikestar View Post
    so the what i have it now, how can i pass in the root into the func without changing my code
    It's a matter of creating an instance of the class and calling the method, which will return the root pointer.

    Code:
    TreeNode foo, *bar;
    
    bar = foo.getRoot(); //returns the root.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    Quote Originally Posted by kmdv
    Code:
    TreeNode* TreeNode::getRoot() const
    {
        TreeNode* root = _parent;
        while (root)
        {
            root = root->_parent;
        }
        return root;
    }
    Code executes until root is null, therefore this function - as written - will always return null.

    [edit]May also be some const issues with that, a const function should return a const pointer, not a non-const pointer.[/edit]
    Last edited by hk_mp5kpdw; 08-31-2010 at 06:49 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    681
    Right, sorry, need to read my code before posting it:

    Code:
    const TreeNode* TreeNode::getRoot() const
    {
        if (_parent)
        {
             return _parent->getRoot();
        }
        return this;
    }
    should be written second non-const version

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. on method pointers and inheritance
    By BrownB in forum C++ Programming
    Replies: 2
    Last Post: 03-02-2009, 06:50 PM
  2. stuck on display method
    By shintaro in forum C++ Programming
    Replies: 2
    Last Post: 02-01-2009, 04:17 PM
  3. Best communication method to thousand childs?
    By Ironic in forum C Programming
    Replies: 8
    Last Post: 11-07-2008, 11:30 PM
  4. C# method
    By siten0308 in forum C# Programming
    Replies: 6
    Last Post: 07-15-2008, 07:01 AM
  5. Overriding a method in C
    By DavidDobson in forum C Programming
    Replies: 1
    Last Post: 07-05-2008, 07:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21