I have a base class that uses template, and it has a few methods that are not dependent on the template type, but when I use the pointer Base* a
instead of the derived class the compiler complains because there is no type specified. I know in java this is possible but not sure if it is possible in C++. Here a simple example:
template <typename T>
class Base {
public:
Base(const T& t) : _t(t) {}
virtual ~Base() { }
void doSomething() { std::cout << "Hello world/n"; }
virtual T getVal() const { return _t; }
private:
T _t;
};
class DerivedA : public virtual Base<std::string>
{
public:
DerivedA(const std::string& path) : Base<std::string>(path) {}
virtual ~DerivedA() {}
};
class DerivedB : public virtual Base<int>
{
public:
DerivedB(int value) : Base<int>(value) {}
virtual ~DerivedB() {}
};
int main(int argc, const char * argv[]) {
DerivedA d("hello world\n");
Base* basePtr = &d; // ERROR: Use of class template 'Base' requires template arguments
basePtr->doSomething();
Thanks in advance