简体   繁体   English

C ++模板继承

[英]C++ Template Inheritance

I couldn't find any similar questions with my Googling' skills, so hopefully SO can help. 我用谷歌搜索的技能找不到任何类似的问题,所以希望SO可以提供帮助。 (Sample header psuedocode below) (下面的示例标题psuedocode)

namespace randomNamespace{
template <typename A, typename B>
class Shape{
    public:
        Shape(int);
        ~Shape();

        bool insert(std::pair<A,B> ab);
    private:
        std::vector<std::pair<A,B> > someListOfPairs;
};
}

class Square : public Shape<string, string>     //causes error requiring template before '<'
{
public:
    Square(int);    //When defined it is Square(int x) : Shape(x); as an initialization list to call superclass constructor
    ~Square();

    bool insert(std::pair<string, string> p);
private:
    //Shape<string, string> someShape    -- see question #3
};

1) What is the proper syntax for inheriting a template'd generic base class? 1)继承模板通用基类的正确语法是什么?

2) Does Class Square have access to someListOfPairs (not talking about it being private), such as when I call Square's insert(pair of strings), it will then call Shape's insert(pair of generic type)? 2)Class Square是否可以访问someListOfPairs(不是说它是私有的),例如当我调用Square的insert(字符串对)时,它会调用Shape的insert(泛型类型对)? (Also how would this actually be implemented??) (这又是如何实现的?)

3) Would I have to define an object of Shape someShape to properly use the Shape class members even though it is already using inheritance? 3)我是否必须定义Shape someShape的对象才能正确使用Shape类成员,即使它已经在使用继承?

1) What is the proper syntax for inheriting a template'd generic base class? 1)继承模板通用基类的正确语法是什么?

There's nothing wrong with the syntax of inheritance, you just need to specify the namespace of your base class: 继承的语法没有任何问题,您只需要指定基类的命名空间:

class Square : public randomNamespace::Shape<std::string, std::string> { ...
                      ^^^^^^^^^^^^^^^

2) Does Class Square have access to someListOfPairs (not talking about it being private), such as when I call Square's insert(pair of strings), it will then call Shape's insert(pair of generic type)? 2)Class Square是否可以访问someListOfPairs(不是说它是私有的),例如当我调用Square的insert(字符串对)时,它会调用Shape的insert(泛型类型对)? (Also how would this actually be implemented??) (这又是如何实现的?)

You could call the insert member function of Shape from the member function of Square in the following way: 您可以通过以下方式从Square的成员函数调用Shapeinsert成员函数:

bool Square::insert(std::pair<std::string, std::string> p) {   
  randomNamespace::Shape<std::string, std::string>::insert(p);
  // ... 
  return true; 
}

3) Would I have to define an object of Shape someShape to properly use the Shape class members even though it is already using inheritance? 3)我是否必须定义Shape someShape的对象才能正确使用Shape类成员,即使它已经在使用继承?

Yes you would have to initialize the base class in the initializer list of Square s constructor: 是的,你必须在Square的构造函数的初始化列表中初始化基类:

Square::Square(int a) : randomNamespace::Shape<std::string, std::string>(a) {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM