What is ->* operator in C++?

前端 未结 3 1335
执念已碎
执念已碎 2020-12-30 00:16

C++ continues to surprise me. Today i found out about the ->* operator. It is overloadable but i have no idea how to invoke it. I manage to overload it in my class but i hav

3条回答
  •  春和景丽
    2020-12-30 00:48

    Just like .*, ->* is used with pointers to members. There's an entire section on C++ FAQ LITE dedicated to pointers-to-members.

    #include 
    
    struct foo {
        void bar(void) { std::cout << "foo::bar" << std::endl; }
        void baz(void) { std::cout << "foo::baz" << std::endl; }
    };
    
    int main(void) {
        foo *obj = new foo;
        void (foo::*ptr)(void);
    
        ptr = &foo::bar;
        (obj->*ptr)();
        ptr = &foo::baz;
        (obj->*ptr)();
        return 0;
    }
    

提交回复
热议问题