Skip to content

Instantly share code, notes, and snippets.

@chenhong805
Last active June 24, 2017 14:54
Show Gist options
  • Save chenhong805/45b135872fde10709df9c6490beddd9e to your computer and use it in GitHub Desktop.
Save chenhong805/45b135872fde10709df9c6490beddd9e to your computer and use it in GitHub Desktop.
template<class T>
class Node {
protected:
T val;
virtual T getVal() { return this.val; }
virtual void setVal(T val) { this.val = val; }
template <typename R, typename V>
virtual Node<R> operator+(Node<V> rhs);
}
class DoubleNode: public Node<double> {
virtual DoubleNode operator+(DoubleNode n){
return new DoubleNode( this.getVal() + n.getVal() ); // handle double + double
}
virtual DoubleNode operator+(IntNode n){
return new DoubleNode( this.getVal() + n.getVal() ); // handle double + int
}
}
class IntNode: public Node<int> {
virtual DoubleNode operator+(DoubleNode n) {
// implementation
}
virtual IntNode operator+(IntNode n) {
// implementation
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment