How do I get around this? I clearly cannot make the value() method virtual as I won’t know what type it is beforehand, and may not know this when accessing the method from b:
class Base
{
public:
Base() { }
virtual ~Base() { }
private:
int m_anotherVariable;
};
template <typename T>
class Derived : public Base
{
public:
Derived(T value) : m_value(value) { }
~Derived() { }
T value() { return m_value; }
void setValue(T value) { m_value = value; }
private:
T m_value;
};
int main()
{
Base* b = new Derived<int>(5);
int v = b->value();
return 0;
}
Compilation errors:
error: 'class Base' has no member named 'value'
This statement:
The variable ‘b’ is being trated like it is an object of Derived<int>.
So tell the compiler:
Note: If b is not a Derived<int> the result of the cast is NULL.
So the following would probably be safer:
Alternatively by using references you get a bad_cast exception thrown:
Or to be nice and obscrure we can do it one line.
But I think you can achieve what you are trying to do with the boost::any