美文网首页
member function pointer

member function pointer

作者: 小雪球的学习笔记 | 来源:发表于2019-06-10 19:49 被阅读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

相关文章

网友评论

      本文标题:member function pointer

      本文链接:https://www.haomeiwen.com/subject/hpmtfctx.html