Knowing how things roll around here somebody is gonna mark this "dupe" right away I'm sure but I sure can't find a good example for "passing member function from constructor". Plenty of passing TO constructors or passing member functions outside, but not from inside a constructor...
Story: We are using a library that uses 'mailboxes', and provides a hook for RX interrupt callbacks. We have our own class for devices. Each object-instance needs to be able to handle one of the libraries mailboxes. Therefore constructor takes a mailbox (maybe > 1 in future), and then registers itself with the comms library.
Simply: I want each objects constructor to pass a pointer to its "Handler" member function to this library.
The problem is of course with member functions vs. standard function pointers. Standard function pointer works fine, a static class function 'works' but we need to work with local object variables not static class variables.
I have read a million threads on this and see the problems between member functions with hidden 'this' arguments vs standard C function pointers etc. etc.
Have attempted to use lambda and std::function<> approaches... Maybe I am fighting the impossible? Is it impossible to pass a pointer to a function before the constructor completes creating the instance?
Example... (there is also a Class hierarchy for code-reuse purposes and there will be some function overrides and extensions in subclasses, but the basic mailbox setup should be in the top base class)
class Base // Object that processes received communication frames
{
public:
Base(int);
bool Initialized;
protected:
fooframestruct rxframe;
bool newframe;
void Handler(const fooframestruct&);
void DoOtherStuff();
};
Base::Base(int rxmailbox) :
Initialized(false),
newframe(false)
{
// point comms library to instance function when data received by its mailbox
somelibraryobject.OnReceive(rxmailbox, this->Handler); // obviously doesn't work cuz member func
}
void Base::Handler(const fooframestruct &thisframe)
{
// callback function - just grab the frame and set a flag
rxframe = thisframe;
newframe = true;
}
void Base::DoOtherStuff()
{
cout << "blah";
Initialized = true;
}
class SubClass : Base
{
public:
using Base:Base;
void ExtendWithOtherStuff();
}
main
{
Base device1 = new Base(1);
Base device2 = new Base(7);
SubClass device3 = new Base(5);
// etc.
}
Naturally compiler failure, invalid use of non-static member function, which as described above, I fully understand why, just not what the best way forwared is.
I realize I could do things like create a secondary class function say
Base.RegisterMB()
and then call that in the main setup such like:
Base device1 = new Base(1);
device1.RegisterMB();
But I hate the '2 step object init'. I suppose a 3rd workaround would be setting the library up outside the Class aka...
somelibraryobject.OnReceive(rxmailbox, device1.Handler);
or at least something like that , but again, I'd rather encapsulate the object setup if possible.
But maybe I can't?