class Int32Param: public IfParam
{
public:
Int32Param();
Int32Param(IfParam* l_param);
int getValue();
void setValue(int l_value);
private:
int value;
};
class StringParam: public IfParam
{
public:
Int32Param();
Int32Param(IfParam* l_param);
const std::string& getValue();
void setValue(const std::string& l_value);
private:
std::string value;
};
My Problem now ist getting access to the getters/setters over the interface.
I wish I could initialize the Params like this:
1 2 3 4 5
IfParam* param = new Int32Param();
param.setValue(12);
IfParam* param = new StringParam();
param.setValue("String");
But to have access to the getter/setter I have to declaire the functions in the interface as generic functions. But how?
I tried to use temlates, but then i have to declaire IfParam as IfParam<int>. Thats a problem because in my original program I do not know which TypeParam the IfParam interface will be initialized with when I create the pointer.
Sorry for my bad english, I am german^^
Thank you very much
Best Regards
Marcel
Okay thank you both.
I read that the use of the dynamic_cast is not really elegant.
@kefin
I didn't know that virtual functions can be overriden either.
But now i have another problem: When I create my virtual functions in the Interface:
Well you're trying to overload the return type. However, overloading works only for arguments. You could try specifying dummy arguments. Another thing is to simply name the functions differently:
1 2
getStringValue()
getIntValue()
In fact, this is what the wxWidgets library seems to use for wxEvents, and it works alright.
you need to overwrite each function in a derived class like StringParam. Whether you want it or not.
you cannot return a local variable in your getValue().
if you want to add another type you need to implement it in the base class and all derived class too.
you probably need an [unnecessary] type variable.
all a kinds of trouble are waiting for you maintaining the mess.
with dynamic_cast you can concentrate on what you really want in your base/derived class without any mess.
if you want something from the derived class it is not a problem