3

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?

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
Christoph
  • 542
  • 4
  • 24
  • 1. Is it possible to post the signature of `Object::Object(...)` 2. Seems like there is a typo in your `static_cast` line, missing `)` before `;`? – CJCombrink Aug 12 '16 at 09:35
  • Possible duplicate of [C++ overloaded method pointer](http://stackoverflow.com/questions/4364599/c-overloaded-method-pointer) – Oktalist Aug 12 '16 at 12:55

2 Answers2

3

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.

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
  • Ok! Thanks! Seems now to be passed correctly, but it seems, my constructor is bad defined: "Conversion of argument 4 "void (__thiscall Status::* )(void)" in "void (__cdecl *)(void)" not possible". My constructor is this one: Object(void (*repaintFCT) (void)); – Christoph Aug 12 '16 at 09:35
  • @Christoph It seems the parameter type of constructor is not correct either. It should be something like `void(myclass::*sth)()`, i.e. `void(Status::*repaintFCT)()` for your real code. BTW: You might have to fix lots of things such as the type of member variable, etc. – songyuanyao Aug 12 '16 at 09:37
  • Ok. But the contructor doesn't know the the class myclass. The inner class can't include the outer class definition outer_class.h. Or am I wrong? – Christoph Aug 12 '16 at 09:41
  • @Christoph The inner class should know outer class. What do you mean can't include? You might post the relevant code, or ask a new question (if it became a new one). – songyuanyao Aug 12 '16 at 09:46
  • If you create an object of class 2 in class you include class2.h in class 1. But when you in include class1.h in class 2, as it is needed here, you get various errors (because if an include loop I think). – Christoph Aug 12 '16 at 09:49
  • 1
    @Christoph I see. You don't have to include, forward declaration would be fine. Add `class myclass;` before the delaration of the class `Object`. – songyuanyao Aug 12 '16 at 09:55
2
  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.)

molbdnilo
  • 64,751
  • 3
  • 43
  • 82