Hi, I'm trying to implement a decision tree that gets doubles as input (in this sample code just random numbers) and returns a boolean value. At the nodes, I'd like to use various operators that can have different input and return types. I've implemented an abstract base class for the nodes and I'm deriving the specific nodes from it. In the code below, I've included only a few derived classes. Building the tree (randomly) is no problem. However, I'm looking for a clever way to evaluate the tree. I think that uncommenting the lines in bold print would in principle do it. However, this is not possible because "value" is not a member of the base class. The type of "value" is different in the derived classes, so I cannot simply declare it in the base class. Can you suggest a nice way to make this work? Should I use a class template? Thanks!
Hi L B, thanks for your suggestions! I see that f is superfluous.
I'm not sure how to let "evaluate" return a value. Depending on the derived class, evaluate should return a different type (boolean or double). So I guess I cannot declare it as a virtual function in the base class. And if it is not declared in the base class, I cannot access it using the "(*Child_1).evaluate();" Probably I'm mistaken at some point...?
You will want to have a Value class that stores what type of information it holds and the information itself, along with functions to get the information as any other type. How do you think scripting languages work? ;)