Wskaźniki do funkcji w klasach.

0

Witam, mam taki prosty programik.

      1 class Class001 {
      2
      3         int (*showfunc)();
      4 public:
      5         int show1() {
      6                 return 1;
      7         }
      8         int show2() {
      9                 return 2;
     10         }
     11         int show3() {
     12                 return 3;
     13         }
     14
     15         int run(int number) {
     16                 switch(number) {
     17                         case 1 : showfunc = show1; break;
     18                         case 2 : showfunc = show2; break;
     19                         case 3 : showfunc = show3; break;
     20                         default: return 0;
     21                 };
     22                 return showfunc();
     23
     24         }
     25 };
     26
     27
     28 int main() {
     29         Class001 object;
     30         object.run(2);
     31 }

Przy kompilacji tego programu, wyskakuja taki oto bledy:

prog001.cpp: In member function `int Class001::run(int)':
prog001.cpp:19: error: argument of type `int (Class001::)()' does not match `int
   (*)()'
prog001.cpp:20: error: argument of type `int (Class001::)()' does not match `int
   (*)()'
prog001.cpp:21: error: argument of type `int (Class001::)()' does not match `int
   (*)()'

Czy moze ktos wie o co jest nie tak??

0

zadeklaruj metody jako static.

0

Jesli cwiczysz sobie wskazniki na metody to:

int (Class001::*showfunc)();

i

case 1 : showfunc = &Class001::show1; break;
...

oraz

return (this->*showfunc)();
0

Wielkie dzieki

1 użytkowników online, w tym zalogowanych: 0, gości: 1