开发者

Pointer to class method

开发者 https://www.devze.com 2022-12-08 08:38 出处:网络
I\'m trying to have pointer to class methods, so I have something like: class foo { public: static void bar() {

I'm trying to have pointer to class methods, so I have something like:

class foo {
    public:
    static void bar() {
}
};

void (foo::*bar)() = &foo::bar;

That doesn't compile :( I get:

> error: cannot convert ‘void (*)()’ to
> ‘void (foo::*)()’ in
&g开发者_JS百科t; initialization


A static method, when used by name rather than called, is a pointer.

void (*bar)() = foo::bar; // used as a name, it's a function pointer
...
bar(); // calls it


A pointer to a static member has the same type as a pointer to non-member.

Try:

void (*bar)() = &foo::bar;


bar() is a static function, in other words there is not this parameter.

void (*myfunptr)() = &(foo::bar);
0

精彩评论

暂无评论...
验证码 换一张
取 消