member function pointer

2019-06-10  本文已影响0人  小雪球的学习笔记

example

#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <functional>
#include <string>


typedef void(*handler)(const char*);

class A {
protected:
    typedef void(A::*handler)(const char* b);
    
public:
    handler a;
    A() {
        a = &A::foo;
    }
    void foo(const char* a) {
        std::cout<< "foo "<<  std::string(a) << std::endl;
    }    
};

class B : public A {

public:
    B() : A() {
        a = (handler)&B::f1;
    } 
    
    void f1(const char* a) {
        std::cout<< "f1 "<<  std::string(a) << std::endl;
    }
};



int main()
{
    A a1;
    a1.foo("hello");
    B b;
    b.f1("hello");
    A* ap = &a1;
    (ap->*(ap->a))("world");
    (a1.*(a1.a))("wold");
    (b.*(b.a))("wold");
}
foo hello
f1 hello
foo world
foo wold
f1 wold
上一篇下一篇

猜你喜欢

热点阅读