所以我尝试:
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>>();
};
但无法获得预期的效果 - 我想在类 data_child getData 函数中只返回 boost::shared_ptr<std::vector<int>>
.怎么办?
最佳答案
我现在看到的解决您问题的唯一方法是:
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>(); }
};
用法:
data_child dc;
dc.getData();
//dc.getData<float>(); // compilation error
关于c++ - 如何为子函数指定模板参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14443092/