I'm trying to get a variety of Nodes to run a defined execute function that is passed in to a constructor and stored (and called) from a function pointer variable.
class Node{
std::string(*execute)();
};
Node::Node(std::string(*funcPointer)()){
execute = funcPointer;
}
I also have several derived classes that all also have an execute funcion
class redNode : public Node{
std::string execute();
};
std::string redNode::execute(){
return "I'm red";
}
class blueNode : public Node{
std::string execute();
};
std::string blueNode::execute(){
return "I'm red";
}
Then, I want to call all of the Node's execute functions.
std::string myFunc(){
return "my Func";
}
Node mynode = new Node(&myFunc);
//other instantiations here...
myRedNode.execute();
myBlueNode.execute();
myNode.execute();
However, trying to call .execute() of myRedNode or myBlueNode doesn't work because the execute variable that's a part of the parent class was never set and it seems to be calling that. Trying to then set the execute variable
myBlueNode.execute = &BlueNode::execute;
gives a error C2659: '=' function as left operand, even when I rename the function I'm setting the variable to.
How do I solve this problem? How do correctly call function with a same name as a parent class function pointer?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire