# Pointer in base class to inheritance function (functors, function pointers?)

## Recommended Posts

acinfo64    122
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
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();
}


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.

##### Share on other sites
ToohrVyk    1596
Quote:
 Original post by acinfo64This should return 24 but it will return 12.
It will return 24.

#include <string>#include <iostream>class base{public:  virtual int foo(void){return 12;}  float a;};class class1 : public base{public:  int foo(void){return 24;}  class1(int kk){k = kk;}  int k;};class class2 : public base{public:  int foo(void){return 34;}  class2(std::string strstr){str = strstr;}  std::string str;};int main(){  class1 * cs1 = new class1(1);  base * csb = cs1;  std::cout << csb->foo() << "\n";}
Writes 24 to standard output.
I cannot use casting for this problem.[/quote]

##### Share on other sites
phresnel    953
I guess you are in C++. Then maybe the problem is simply a bug in your code. For the next time, please actually copy+paste source code instead of writing pseudo-code. Also, you might want to strip out code-blocks that are not used in the example you give [smile]

The following prints two times "24" on my box, compiled with g++:
#include <iostream>class base {public:    virtual int foo(void){return 12;}};class class1 : public base {public:    int foo(void){return 24;}};int main() {   class1 * cs1 = new class1;   std::cout << cs1->foo() << std::endl;   base * csb = cs1;   std::cout << csb->foo() << std::endl;}

##### Share on other sites
HomerSp    126
Indeed, that will print 24 and not 12.. I believe you should be able to call the base foo by using a cast, but I'm at work so I can't check.

##### Share on other sites
phresnel    953
Quote:
 Original post by HomerSpIndeed, that will print 24 and not 12.. I believe you should be able to call the base foo by using a cast, but I'm at work so I can't check.

Not a cast, but with the member selection operator :: :
int main() {   using namespace std;   class1 * cs1 = new class1;   cout << cs1->foo() << '\n'; // --> 24   cout << ((base*)cs1)->foo() << '\n'; // --> 24   cout << static_cast <base*> (cs1)->foo() << '\n'; // --> 24   cout << reinterpret_cast <base*> (cs1)->foo() << '\n'; // --> 24, btw: don't do this.   cout << cs1->base::foo() << endl; // --> 12 :)   //edit{   cout << ((base)(*cs1)).foo() << std::endl; // --> 12, but this is mad, foo() cannot change the state of cs1.   cout << (&(base)(*cs1))->foo() << std::endl; // --> 12, but this is maddest, derefering the address of the *copy*   //}edit   delete cs1;}

edit: Added mad examples (last one in code). I called it "mad", because it will construct a copy of cs1, instead of directly using it, so any function called through this typecast won't be able to change the state of the original. The "maddest" example is nonsense, as we are dereffering the address of the copy.

Just wanted to complete the Cast-Conjecture [smile]

[Edited by - phresnel on December 16, 2008 7:54:00 AM]

##### Share on other sites
acinfo64    122
Thank you all very much for your help, it was very helpful.

I pasted pseudo-code because the original code was to long. The idea was that the code was wrong and I was on the path of using function pointers or functors and then I get messed up, because I didn't now how to use them in this case. (Transform this code to functors, what's still not succeeded.).

The problem was that the function call was very slightly different.