当child是模板时,从父级调用子方法

时间:2017-11-26 16:37:32

标签: c++ templates

我有这个大学项目,我必须使用模板创建一个BinarySearchTree类。我们必须根据文件中的数据类型读取文件并创建树。我为名为BST的树创建了一个父类,因此我可以使用树而不给它类类型。

class BST{
    public:
        BST();
        ~BST();
}

template <class T> class BinarySearchTree : public BST{
    public:
        void add(T val);
}

我想这样做:

BST tree = BinarySearchTree<int>(); //just an example, it can be of any type
tree.add(5); //doesn't work

如何在不提供特定变量类型的情况下从BST调用“add”?

1 个答案:

答案 0 :(得分:-1)

使用CRTP(奇怪的递归/重新模板模板模式)和概念模式。

https://qiita.com/Riyaaaa_a/items/a9af401520f238f45b80

这是用日文写的,但很有意思!