Function pointers

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 ?

Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Function pointer type definition function will return "void"
typedef void(*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 
	virtual void DataHandler(void) { }
};


Thanks in advance
Last edited on
closed account (48bpfSEw)
void
Last edited on
"Necip" your code doesn't make sense what do you mean by
ActualClass(void) : Serialisable() {} it is not even related with my question
Last edited on
closed account (48bpfSEw)
void
Last edited on
It doesn't have to be called ! it's default and this is not the problem READ THE QUESTION !!!!!!

I've ran into problem I have an error witch reads:
"Return value type does not match the function type"
Last edited on
closed account (48bpfSEw)
http://www.cprogramming.com/tutorial/function-pointers.html
DataHandler, because it is a member function, actually takes an implicit "this" parameter that you need to account for in the function type.
Zhuge
thank you very much
Zhuge

now I have compilation errors

I've tried following:

&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.
Last edited on
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
Wrap the call in a polymorphic call wrapper. http://en.cppreference.com/w/cpp/utility/functional/function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <functional>

using DataHandlerFunction = std::function< void() > ;

struct base
{
    virtual ~base() = default ;
    virtual DataHandlerFunction RegisterData() = 0;

    void call_it() { DataHandlerFunction fn = RegisterData() ; fn() ; }
};

struct derived : base
{
    virtual DataHandlerFunction RegisterData() override { return [this]() { this->DataHandler() ; } ; }

    virtual void DataHandler() { std::cout << "derived::DataHandler()\n" ; }
};

struct another_derived : base
{
    virtual DataHandlerFunction RegisterData() override
    { return []() { std::cout << "data handler for another_derived\n" ; } ; }
};

int main()
{
    derived d ;
    base& b = d ;
    b.call_it() ; // derived::DataHandler()

    another_derived d2 ;
    base& b2 = d2 ;
    b2.call_it() ; // data handler for another_derived
}

http://coliru.stacked-crooked.com/a/6a6d912a6d6fd989
Topic archived. No new replies allowed.