using pointer to member function as argument

Hi -

I'm working on a FreeRTOS project, and I'd greatly like to take advantage of C++ where possible. I'm currently trying to create and use a pointer to a member function, so that I can pass the pointer as an argument in a FreeRTOS call.

The compiler is giving me errors for my code in the function call...can someone tell me what I'm doing wrong?

Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
class Led
{
public:
    void init_led();
    void led_task(void *p) __attribute__ ((noreturn));
    void (Led::* p)(void *) = &Led::led_task;
};
.
.
.
    Led led;
    led.p = &Led::led_task;
    xTaskCreate(led.p, "led_task", 2048, nullptr, 5, &ledTaskHandle);

Last edited on
A pointer to a member function is a special kind of pointer that can't be used without an object of the correct type (I believe it's more of an offset than an address). If you need to pass a regular function pointer then you need to use a static function of the class (or a function outside of a class).
Last edited on
Interesting...but if I declare the function led_task() as static, the compiler then complains about my pointer:


Semantic Issue
21:25: error: cannot initialize a variable of type 'void (Led::*)(void *) __attribute__((thiscall))' with an rvalue of type 'void (*)(void *)'


Heh...this really shouldn't be so complicated...
Too bad I can't see that line from here.
If it's just the initialization of p then obviously it shouldn't be declared as a pointer-to-member function anymore.
 
void (Led::* p)(void *) = &Led::led_task;


Are you trying to store a function pointer in a class?
You can't do something like that, that way.

here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class myclass{
public:

 // we are defining a function pointer what we later on can set
void(*myFunction)(void);

// static function in a class would mean that the function has a structure to it
// instead of working more like a inline ( macro, replaced with compiled ), 
// it works exactly like function would.
static void StaticClassFunction(){
    cout << "hello world" << endl;
}

myclass(){
    // here we are setting a function pointer, we can do it anywhere, doesn't have to
    // be class construction function. we can do it in main() as well.
    myFunction = StaticClassFunction();
}
}

myclass foo;
foo.myFunction();


and we get the output:

hello world


or maybe you meant a virtual function?
I'm little confused cause of your code, it looks like a c++ but it isn't quite.

Edit:
One important thing to remember. Class functions do not actually exist unless they are marked as static. Class functions are like inline functions, they will be replace with the code with in the function on compile time.

You can't also define anything in a class unless it's in the function or unless its a virtual.
I won't go into them cause I feel it's not what you want.
Last edited on
Here's a couple of attempts, with their corresponding error messages:

1
2
3
4
5
6
7
8
9
class Led
{
public:
    void init_led();
    static void led_task(void *p);
    void (Led::* p)(void *) = &Led::led_task;
    void pp(void *) = &Led::led_task;    // error: cannot convert 'void (*)(void*)' to 'void (Led::*)(void*)' in initialization
     void pp(void *) = &Led::led_task;    // error: invalid pure specifier (only '= 0' is allowed) before 'Led'
};


Thanks...
Last edited on
Fadey: I'm not committed to my original approach; as I mentioned in my first post, I'm just trying to use some C++ features for this FreeRTOS application. Frequently I'll need to furnish function addresses to system calls (as callbacks, etc). and I was hoping I could use an object function for this, but I'm starting to understand that I can't do that.
Generally in cases where C uses pointer functions in classes, the equivalent in C++ would be to use polymorphism.

But the problem is how helpful is it to use OOP, how many classes will you make and how meaningfull are they. Like do you use the same member variables for all the functions?

does this help?
https://www.freertos.org/FreeRTOS_Support_Forum_Archive/July_2010/freertos_Is_it_possible_create_freertos_task_in_c_3778071.html
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

class Led {
public:
    static void led_task(int n);
    void (*p)(int) = led_task;
};

void Led::led_task(int n) {
    std::cout << n << '\n';
}

int main() {
    Led led;
    led.p(42);
    return 0;
}


But simply cramming things into "objects" for no reason is not particularly useful.
Hi poteto - thanks for the link. That does clarify what I can and cannot do. Unfortunately, I can't get the wrapper function technique to compile. I may send a message to the author, as there appears to be at least one error in his code.
Topic archived. No new replies allowed.