C++ Template Inheritance

I couldn't find any similar questions with my Googling' skills, so hopefully SO can help. (Sample header psuedocode below)

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?

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)? (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?

Answers


1) What is the proper syntax for inheriting a template'd generic base class?

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)? (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:

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?

Yes you would have to initialize the base class in the initializer list of Squares constructor:

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

Need Your Help

Pure CSS3 Vertical Accordion

css css3 accordion

I am having trouble figuring out a way to keep the tab open when a header is selected.

Search combo box with "All" or "Any" option

sql combobox

In MS Acccess I have a column in my table that uses a drop down menu to select the value for the field, with six possible options. I am trying to make the same drop down menu/combo box in my form s...