Function pointers

May 17, 2016 at 1:42pm
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 May 17, 2016 at 1:46pm
May 17, 2016 at 2:04pm
closed account (48bpfSEw)
void
Last edited on May 17, 2016 at 5:03pm
May 17, 2016 at 2:12pm
"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 May 17, 2016 at 2:25pm
May 17, 2016 at 2:15pm
closed account (48bpfSEw)
void
Last edited on May 17, 2016 at 5:04pm
May 17, 2016 at 2:18pm
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 May 17, 2016 at 2:20pm
May 17, 2016 at 2:19pm
closed account (48bpfSEw)
http://www.cprogramming.com/tutorial/function-pointers.html
May 17, 2016 at 2:31pm
DataHandler, because it is a member function, actually takes an implicit "this" parameter that you need to account for in the function type.
May 17, 2016 at 2:34pm
Zhuge
thank you very much
May 17, 2016 at 2:52pm
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 May 17, 2016 at 2:53pm
May 17, 2016 at 2:57pm
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
May 17, 2016 at 4:51pm
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
May 17, 2016 at 5:18pm
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.