After educating myself on templates, I'm going to try and explain myself better. What I need is to store the user's member function pointer so that I can call that pointer in my code automatically at a later time.
In this code, I tried to make a template struct to store the necessary data
class myclass
{
private:
template struct storage
{
T* UserObject;
void (T::*MemberFunction)();
};
storage userdata;
//i know this is wrong, but it's for illustration
public:
myclass();
template void attach2(T* objectptr, void (T::*mptr)(), float arg)
{
userdata.UserObject = objectptr;
userdata.MemberFunction = mptr;
//store pointers to call member function later
doSomething(arg);
doSomethingElse();
InterruptIn.rise(objectptr, mptr);
}
void somefunction()
{
userdata.UserObject->userdata.MemberFunction();
}
};
but this obviously won't work because userdata doesn't know what type to use. The only time it can know this is if it is used to create an object within the attach2 block when the user passes the type in. That won't work though, because the data is lost when the object goes out of scope when attach2 terminates.
How do the classes "Serial" and "InterruptIn" accomplish this? I'm sure they need to store tptr and mptr somehow (unless I'm mistaken again). Does anyone know how to do this?
Thank you to everyone who helps.
hello,
I'm working on a project that requires the incorporation of InterruptIn in a class. I need to wrap attach in a member function, and the user must pass in the address to the object and that of the object's member function to attach.
How do you do this? What data type does the function prototype specify for its object address and function address arguments? The InterruptIn class has a "T" pointer in the API.
I would like to do this:
but I don't have access to the "T" datatype.I would also like to use this to store the addresses for use by other members, but again, the "T" eludes me.
Thank you for the help.