Hello everyone I'm working on serialization mechanism so far I've implemented one with inheritance and polymorphism, however this creates a certain problems witch can be solved by function pointers. What I'm planning to do is implement an abstract "Serialisable" class with couple pure virtual method members one of them
will allow deriving object to register data to be serialised and return a function pointer to DataHandlerFunction() witch will be invoked from the serialiser however I've ran into problem I have an error witch reads:
"Return value type does not match the function type"
can any one have a look at my code and tell me what I'm doing wrong ?
// Function pointer type definition function will return "void"
typedefvoid(*DataHandlerFunction)(void);
// Abstract object witch all serialisable object must inherit from
class Serialisable
{
public:
Serialisable(void) {}
virtual ~Serialisable(void) {}
// pure virtual method must be implemented in deriving objects
virtual DataHandlerFunction RegisterData(void) = 0;
public:
// additional data ommited
};
// test class inherits from Serialisable
class ActualClass: public Serialisable
{
public:
ActualClass(void) {}
virtual ~ActualClass(void) {}
// Register data method implementation
virtual DataHandlerFunction RegisterData(void)
{
// object implementing this method registers data here
// return Data handler function
return DataHandler; // error "Return value type does not match the function type"
}
// Data handler method to be returned
virtualvoid DataHandler(void) { }
};
&this->DataHandler // generates error C2276: '&': illegal operation on bound member function expression
&ActualClass::DataHandler // "Return value type does not match the function type"
Any ideas why ? I've just started to learn function pointers I have no problem with "no member functions" but I'm having problems with member functions etc.
Pointers to methods are non as simple as normal function pointers, so it's understandable that you're confused. This page has a lot of useful information about your current problem plus a lot more; take a look: https://isocpp.org/wiki/faq/pointers-to-members
Right they ether have to be static or non member functions as member function pointers points to "relative address" of where the function is in the class rather than the "cctual address" this is why I'm getting conversion error