class Base
{
public :
virtual ~Base() { };
virtualvoid Print() const = 0;
//This function is supposed to access Value() function in the Child class
/*something*/ GetChildValue() const;
};
template< typename T >
class Child : public Base
{
protected :
const T* value;
public :
Child( const T* value ) : value( value ) { }
~Child() { }
T Value() { return *value; }
void Print() const { std::cout << *value << "\n"; }
};
The problem is I don't know how to define member function GetChildValue() in the Base class. So how?
The return type of a function must be known at compile time. Unless you make it return some type of wrapper (such as std::any) you will not be able to return the value without the caller knowing what type he wants to be returned.
If you make GetChildValue() a function template you could let the caller pass the type that he wants as a template argument and then use dynamic_cast to convert the Base into the correct Child type so that you can call the correct Value() function.