need access to Class Object via Function Pointer - Binary Search Tree Class Creation Related
-
03-07-2019 - |
Question
Creating Traversals for Binary Search Tree with Recursion.
void inOrder(void (*inOrderPtr)(T&))
{
if(this->left != NULL)
inOrder((*inOrderPtr)(this->left));
inOrderPtr(this->data);
if(this->right != NULL)
inOrder((*inOrderPtr)(this->right));
}
Here is the function. Now this is obviously wrong. This function is called like this:
first.inOrder(print_vals);
first is the object, and print vals is simply a function that prints what is the data in the object. There are three values for each object, data, left, and right. How do I actually access those items with the function?
Solution
It looks like the call to inOrderPtr(this->data)
is passing just the data
member of the tree node to the print_vals
function. If you would like to access the left
and right
elements, use inOrderPtr(*this)
. You will have to change various declarations in order for this to compile, such as the declarations for inOrderPtr
and print_vals
. Without seeing the rest of your code it's hard to say what you need to change them to.
On another note, it seems to me that you might want to write the recursive calls more like this:
this->left->inOrder(inOrderPtr);
I am making assumptions about your implementation, though.