Search code examples
c++templatesinheritancevirtual-functionsfunction-templates

How to specify template argument for a function in a child?


So I try:

class data_ppp {
public:
    template <class T>
    virtual boost::shared_ptr<T> getData()
    {
        return boost::shared_ptr<T>(new T());
    }
};

class data_child : public data_ppp {
public:
    template<>
    getData<std::vector<int>>();
};

but cant get desired effect - I want to have in class data_child getData function that would only return boost::shared_ptr<std::vector<int>>. How to do such thing?


Solution

  • The only solution to your problem that I see now is:

    class data_ppp
    {
    public:
        template<class T>
        std::shared_ptr<T> getData()
        { return std::shared_ptr<T>(new T()); }
    };
    
    class data_child : public data_ppp
    {
    public:
        std::shared_ptr<int> getData() 
        { return data_ppp::getData<int>(); }
    };
    

    Usage:

    data_child dc;
    dc.getData();
    //dc.getData<float>(); // compilation error