I have some base class A, and some number of inherited classes:
class B : public A
class C : public A
class D : public A
I have function with parsing some config file, which use name in config call:
void do_smth<B>(...);
void do_smth<C>(...);
void do_smth<D>(...);
I have map<A*> objs; and in those functions happend:
void do_smth<T>(...)
{
T *control = new T();
...
objs[name] = T;
}
So i have map of pointers to A. In other function I got name, and i need to get value stored in B,C or D classes.
Problem is that i can’t add virtual function to any class which will return those value, because of A,B,C,D from library and B have function value(), C – text(), D – currentValue() etc. And i need function get_value() which will return result of value(), text(), currentValue() depending on class?
What can you advise me to solve my problem?
I see 2 ways:
1) I can write function from pointer to classes B,C,D and call function like that get_value((B*)objs[name]); but i think it’s not good.
2)I can create boost::bind object with function i need at the create B,C,D variable.
What way will be better, or could you advise me smth better?
So you have
and don’t know what type it is. Try
dynamic_cast: