Member function templates (like your getData()
) cannot be virtual. However, you can have a class template with virtual member functions:
template <class T>
class data_ppp {
public:
virtual boost::shared_ptr<T> getData()
{
return boost::shared_ptr<T>(new T());
}
};
THis allows quite a lot of customization.
1) you can define a class data_ppp< std::vector<int> >
. If that class needs to behave as a generic T
, then you are done.
2) If you want to override behavior for specific data uses but for all types T
and you want to use the new functionality dynamically, you can derive from data_ppp<T>
template <class T>
class data_child: public data_ppp<T> {
public:
virtual boost::shared_ptr<T> getData()
{
// add logging, printing or whatever you want
return boost::shared_ptr<T>(new T());
}
};
3) If you only want to redefine getData()
for T
equal to std::vector<int>
, you only have to specialize data_ppp
template <>
class data_ppp< std::vector<int> > {
typedef std::vector<int> T;
public:
virtual boost::shared_ptr< T > getData()
{
// add logging, printing or whatever you want
return boost::shared_ptr<T>(new T());
}
};