Original Post
Dear all, How can I use the right function of the inherited class, if I have the base class. I tried to use function pointers or functor but I don't know how to use them in the following case like E.g csb->foo(); This should return 24 but it will return 12. So I need a construction, that I can use as variable (csb) the base class but that base class should hold a pointer to the right foo or something like that. I cannot use casting for this problem.
class base
{
public:
virtual int foo(void){return 12;}
float a;
}
class class1 : public base
{
public:
int foo(void){return 24;};
class1::class1(int kk){k = kk;};
int k;
}
class class2 : public base
{
public:
int foo(void){return 34;};
class2::class2(string strstr){str = strstr;};
string str;
}
int main()
{
class1 * cs1 = new class1(1);
........
base * csb = cs1;
csb->foo();
}