-2

Hey I'm trying to cast so I can call a callback from a different class. Not sure why it's not allowing the cast.

authScreen = new AuthScreen();
authScreen->setPtr(&EditorBaseComponent::editorCallback); <-- Cannot initialize a parameter of type 'void (*)(bool)' with an rvalue of type 'void (EditorBaseComponent::*)(bool)'
UpAndAdam
  • 4,515
  • 3
  • 28
  • 46
user809260
  • 29
  • 5
  • 5
    Because you're not passing a pointer to a function, but a pointer to a non-static class method. Which is not a function. There are several different ways to fix this, unfortunately the correct fix depends entirely on each individual situation, but because the shown code fails to comply with Stackoverflow's requirements for a [mre], it's not possible to suggest anything specific, here, except to refer you to your C++ textbook for the description of the differences between a function and a class method. Once the underlying core C++ principle is understood you should be able to devise a fix. – Sam Varshavchik Aug 31 '23 at 21:02
  • What is `AuthScreen`? – Paul Sanders Aug 31 '23 at 21:06
  • Side note: [Why should C++ programmers minimize use of 'new'?](https://stackoverflow.com/q/6500313/4581301) – user4581301 Aug 31 '23 at 21:09
  • Which instance of `EditorBaseComponent` (or instance of a class derived from `EditorBaseComponent`) should the callback be called upon? – Ted Lyngmo Aug 31 '23 at 21:13
  • Side note: Don't try to fix this with an explicit cast. The cast will force the compiler to allow the code to compile, but at runtime you'll see firsthand that even in software you can't jam the square peg in the round hole. Any time you have to cast a function to make it fit whatever the interface is asking for, you've probably already lost. – user4581301 Aug 31 '23 at 21:13
  • Once you've followed Sam's advice, give [Pointers to Member Functions](https://isocpp.org/wiki/faq/pointers-to-members) a read to help you get a handle on some of the thornier implications. – user4581301 Aug 31 '23 at 21:14

0 Answers0