Search code examples
c++classprivate

C++ Sending a private class variable into a function


I'm writing a program dealing with binary search trees in C++. I'm using a .h file that contains a class "treeNode" and I have a function that searches the tree for a specific number and returns a boolean as to whether the number was found or not. My problem is that I have a private class variable for the "treeNode" class called "root" that is a pointer and points to the first element within the tree. I need to somehow send that variable into the search function from the .cpp file as a function parameter.

How do I do this? Every time I try, I get an error telling me that it's a private class variable. Classes confuse me a little.

Thanks!


Solution

  • You can refer this skeleton and build your classes.

    class treeNode
    {
    private:
        int data;
    };
    
    class tree
    {
    private:
        treeNode* root;
    
    public:
        bool search(int data)
        {
        //root is accessible here. No need to pass as argument
        }
    };