简体   繁体   中英

Pass a function pointer of an overloaded member function?

I want to pass a function pointer of my function repaint() which is overloaded in 3 versions. I want to pass the one without any arguments:

void repaint()

I tried:

myObject = new Object(&myclass::repaint);

But the compiler says "I don't know which Version to choose". OK.

Then I tried

myObject = new Object(static_cast<void(*)(void)>(&repaint);

Then I got (sorry for the bad translation):

  • "invalid operation on an expression of a bound member function"
  • "myObject::myObject no overloaded function accepts 3 arguments"

How to pass it correctly?

Member function pointer and non-member function pointer are not the same thing. The type for member function pointer in your code is not correct, change it to

myObject = new Object(static_cast<void(myclass::*)()>(&myclass::repaint);
                                       ~~~~~~~~~

BTW: The void in parameter list is redundant.

  1. You dropped the class scope for some reason - use &myclass::repaint to get a pointer-to-member, like in your first code.
  2. The type of the member function is void (myclass::*)() .
    All pointer-to-member types specify the class.

(The parameter list (void) is a C-ism. Prefer to leave it empty, unless you want to look really old.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM