I'm using a 3rd party proprietary software package. It uses a data model that looks like this:
class Base {
...
}
template<class T>
class Derived: public Base {
protected:
T _t;
public:
T& getData();
}
When I interact with their code, they hand me pointers to Base
objects. I'd like to write some templated functions of my own. How can I do this? i.e. if I knew the type T, I could cast it, but what if I don't know the type? What I'd like is a function looking something like this:
template<T>
DataToString(Derived<T> d){
std::stringstream ss;
ss << d.getData();
return ss.str();
}
which may be called: Base b; std::cout << DataToString(b);
but when I try that, the compiler tells me it can't match the templates. What I've got right now is a "guess and check" if/else block for each data type and I"m wondering if there's a better way.
I think my question sort of related to this, but in my case I'm using a 3rd party library.