Pass function Pointers as argument

Oct 15, 2019 at 11:42pm
I want to pass a function pointer, which is an argument, to another function taking a function pointer as an argument too

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
  class ClassTest
{
public:
    void print();
    void call1();
    void call2 (auto (ClassTest::*funcp)());
    void call3 (auto (ClassTest::*funcp)());
};

void ClassTest::call1()
{
    call2(&print);
}

void ClassTest::call2 (auto (ClassTest::*funcp)())
{
    call3((this->*funcp)());
}

void ClassTest::call3 (auto (ClassTest::*funcp)())
{
        ( this->*funcp)();
}

void ClassTest::print ()
{
    cout << "Ok, it worked";
}


I want to call the function call3 from the function call2 passing it the function pointer.
The way I have written is giving me errors and I konow it's wrong, I just don't know how to do it, even if it's possible

Appreciate any help
Thanks
Oct 15, 2019 at 11:55pm
Member function pointers are not function pointers. See the lengthy discussion here:
http://www.cplusplus.com/forum/general/263275/

As is mentioned in that thread, use a library utility like std::function to solve the problem instead.
Oct 16, 2019 at 9:14am
@mbozzi thank you for the link, that had some very useful insights that I will have to do some research because I realized that I don't actually understand them.

From what I understood reading the thread answers, I am using function pointers, which is discouraged in C++ as it is a C code. I will do some reasearch and see if there is a C++ way of better way of doing this.

However, in the meantime my problem still stands and if someone knows how to do(in a very C++ way) what im trying to do it would be of a great help to point it out to me.
Oct 16, 2019 at 11:06pm
I just want to say that I got it working

I edited this line in function call2

call3((this->*funcp)());

to just

call3(funcp);
Last edited on Oct 16, 2019 at 11:07pm
Topic archived. No new replies allowed.