Hi!
I'm new to generic class programming so maybe my question is silly - sorry for that. I'd like to know whether the following thing is possible and - if so, how to do it
I have a simple generic class Provider, which provides values of the generic type:
template <class A_Type> class Provider{
public:
A_Type getValue();
void setSubProvider(ISubProvider* subProvider)
private:
A_Type m_value;
ISubProvider* m_subProvider;
};
The getValue function shall return m_value in case of m_subProvider is NULL. But if SubProvider is not Null, the value shall be calculated by the SubProvider class.
so subprovider must be of generic type too, but i create it as an abstract class without implementation:
template <class A_Type> class ISubProvider{
public:
virtual A_Type getValue() = 0;
};
now I want the actual implementations of ISubProvider to be nongeneric! for example I want to implement IntegerProvider which returns type Integer
class IntegerProvider : public ISubProvider{
int getValue(){return 123;}
};
and maybe a StringProvider:
class StringProvider : public ISubProvider{
string getValue(){return "asdf";}
};
now - how can I code the whole thing, such that i can use the
void setSubProvider(ISubProvider* subProvider)
function of class Provider only with a subprovider that corresponds to the generic type of Provider?
for example, if i instanciate a provider of type int:
Provider<int> myProvider = new Provider<int>();
then it shall be possible to call
myProvider.setSubProvider(new IntegerProvider());
but it must be impossible to call
myProvider.setSubProvider(new StringProvider());
I hope you understand my question and can tell me how to create that code properly :)
Thank you!