The function expects arguments and returns a pointer to the void function

I have several void functions that do some important things in my code.

void function1(Myclass class1, int myvar)
{
   // do some stuff
}

void function2(Myclass class1, int myvar)
{
   // do some other stuff
}

// ... maybe some more similar functions

I want to create a function that returns a pointer to any of these functions depending on the arguments I pass. I do not know how to do that. I want to have something like

void* choosefunction(int i, int j)
{
   if (i == j) return (void*)function1;
   else return (void *)function2;
}

Then I would just call them using this pointer.

void *(*ptrProcFunc)(int,int); 
ptrProcFunc = &choosefunction;
(*ptrr)() = ptrProcFunc(i,j);
ptrr(class1,myvar);

How to do it right? Thank.

+4
source share
5 answers

typedef - your friend.

typedef void (*func_ptr)(Myclass, int);

func_ptr choosefunction(int i, int j)
{
   if (i == j) return &function1;
   else return &function2;
}

Then:

func_ptr ptrr = choosefunction(i,j);

ptrr(class1,myvar);
+6
source

Here is a complete example.

#include <functional>
#include <iostream>

// Typedef for convenience, called "fcn".
typedef void(*fcn)(int, int);

// You could also use c++11 std::function, which is easier
// to read than function pointers.
typedef std::function<void(int, int)> modern_fcn;

void func1(int a, int b) {
    std::cout << "func1" << std::endl;    
}

void func2(int a, int b) {
    std::cout << "func2" << std::endl;
}

// This returns our typedef'd function pointer.
fcn pick(int i, int j) {
    if (i == j) {
        return &func1;
    } else {
        return &func2;
    }
}

int main()
{
  // Should call func1.
  pick(1,1)(3, 5);

  // Should call func2.
  pick(1,2)(3, 5);
}
+1
source

:

  • using, :

    using func_ptr = void (*)(Myclass, int);
    using choose_ptr = void *(*)(int,int);
    
    // ...
    
    func_ptr choosefunction(int i, int j) {
        if (i == j) return &function1;
        else return &function2;
    }
    
    // ...
    
    choose_ptr ptrProcFunc = &choosefunction;
    func_ptr ptrr = ptrProcFunc(i,j);
    ptrr(class1,myvar);
    
  • auto (++ 14):

    auto choosefunction(int i, int j) {
        if (i == j) return &function1;
        else return &function2;
    }
    
    // ...
    
    auto ptrProcFunc = &choosefunction;
    auto ptrr = ptrProcFunc(i,j);
    ptrr(class1,myvar);
    
  • auto (++ 11):

    auto choosefunction(int i, int j) -> decltype(&function1) {
        if (i == j) return &function1;
        else return &function2;
    }
    
    // ...
    
    auto ptrProcFunc = &choosefunction;
    auto ptrr = ptrProcFunc(i,j);
    ptrr(class1,myvar);
    

    , function1 function2 .

+1

()

void (*choosefunction(int i, int j))(Myclass, int)
{
   if (i == j) return &function1;
   else return &function2;
}

using ( typedef) :

using F = void(Myclass, int);
using FPtr = void(*)(Myclass, int); // or using FPtr = F*;

F* choosefunction(int i, int j);

FPtr choosefunction(int i, int j);

, ++ 14 auto:

auto choosefunction(int i, int j) {
    if (i == j) return &function1;
    else return &function2;
}

, :

FPtr f = choosefunction(i, j);

f(myclass, myvar);
+1

( , C ++)) , "". , , ( , , , ). , Myclass int .

, . using typedef, :

using func_ptr = void (*)(Myclass, int);

( ) , :

int a

, .

:

int sum(int a, int b)

() : sum, , , .. int. , , " int", , , OP.

sum - ", ".

To help you break the habit, write, or at least think about functions in the syntax for declaring a new return type:

auto sum(int a, int b) -> int
+1
source

Source: https://habr.com/ru/post/1654513/


All Articles