Calling a function in a third object based on the combination of subclasses as parameters

Viewed 60

I'm planning to represent a list of nodes.
My base object Node can be extended in order to represent different future representations and rules.
For instance, Node1 and Node2 are my derived objects.

class Node {};

class Node1 : public Node {};

class Node2 : public Node {};

For customization purposes, I'm letting a third object called Rule have the overloaded function calc for specific extensions of Node

This is how I imagine the Rule object to look like:

class Rule : public <some not yet defined pure virtual> {
public:
  static void calc(Node n1, Node n2){
    cout << "Node : Node" << endl;
  }
  static void calc(Node1 n1, Node n2){
    cout << "Node1 : Node" << endl;
  }
  static void calc(Node n1, Node1 n2){
    cout << "Node : Node1" << endl;
  }
  static void calc(Node2 n1, Node n2){
    cout << "Node2 : Node" << endl;
  }
  static void calc(Node n1, Node2 n2){
    cout << "Node : Node2" << endl;
  }
  static void calc(Node1 n1, Node2 n2){
    cout << "Node1 : Node2" << endl;
  }
  static void calc(Node2 n1, Node1 n2){
    cout << "Node2 : Node1" << endl;
  }
}

It is intended that the base object of Rule can be a pure virtual and must be extended or its extensions can be extended multiple times in order to include function overloads for the newly defined extensions of Node

How can I call the correct function by passing the objects contained in a array/vector/list of the base object?

Node n[2];
n[0] = Node2();
n[1] = Node1();

Rule().calc(n[0], n[1]);

Desired output: Node2 : Node1

Actual Output: Node : Node
(obviously, I'm unable to cast objects in such state)

Perhaps I should mention that in my particular case, one single defined Rule object is likely going to be passed between Node objects and end up in recursive calls as I'm attempting to some sort of propagation

  • How can I achieve this?
  • Why this design doesn't/can't work?
    • Can it be fixed?
  • Does this require a complete code structure rethink?
    • If yes, please introduce me to the correct way

0 Answers
Related