C++ member function pointer for someclass.

#include <stdio.h>

class SomeClass {
 public:
     void some_member_func(int x, char *p) {
       printf("In SomeClass"); };
};

class DerivedClass : public SomeClass {
 public:
 // If you uncomment the next line, the code at line (*) will fail!

//     void some_member_func(int x, char *p) { printf("In DerivedClass"); };

};

int main() {
    // Declare a member function pointer for SomeClass

    typedef void (SomeClass::*SomeClassMFP)(int, char *);
    SomeClassMFP my_memfunc_ptr;
    my_memfunc_ptr = &DerivedClass::some_member_func; // ---- line (*)

    DerivedClass *dc;
    dc->*my_memfunc_ptr(1,"");
}

 

 

#include <stdio.h>

struct X {
  int foo() { return 0; }
} x;

struct Y
{  
    static int (X::*p)();
};

int (X::*Y::p)()=&X::foo;

int main(int argc, char *argv[])
{
    printf("Hello, world\n");
    
    (x.*Y::p)();
    // (x->*Y::p)();
    // (Y::p)(x);
    // (Y::x.*p)();
    // (x.Y::p)();  


    return 0;
}
 

Powered by Jekyll and Theme by solid

本站总访问量