Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a function with default parameter through a pointer to function that is the return of another function?

I have functions Mult, Add, Div, Sub, Mod those takes two integers and returns the result of its parameters. And a function Calc that takes a character as an Operator and returns a pointer to function that returns an integer and takes two integer parameters like Mult.

  • Functions like Mult's second parameter is default So when I call Calc, Calc returns the address of Mult or Add... depending on the value of parameter of Calc thus I can pass only one argument.

But It doesn't work with pointer to function:

int Add(int x, int y = 2) { // y is default
    return x + y;
}

int Mult(int x, int y = 2) { // y is default
    return x * y;
}

int Div(int x, int y = 2) { // y is default
    return y ? x / y : -1;
}

int Sub(int x, int y = 2) { // y is default
    return x - y;
}

int Mod(int x, int y = 2) { // y is default
    return y ? x % y : -1;
}

using pFn = int(*)(int, int);


pFn Calc(char c) {
    switch (c) {
        case '+':
            return Add;
        case '*':
            return Mult;
        case '/':
            return Div;
        case '-':
            return Sub;
        case '%':
            return Mod;
    }
    return Mult;
}

int main(int argc, char* argv[]){

    pFn func = Calc('%');
    cout << func(7, 4) << endl; // ok
    //cout << func(7) << endl; // error:  Too few arguments
    cout << Mult(4) << endl; // ok. the second argument is default

    func = Calc('/'); // ok
    cout << func(75, 12) << endl; // ok

    std::cout << std::endl;
}

Above if I call Mult with a single argument it works fine because the second argument is default but calling it through the pointer func it fails. func is pointer to function that takes two integers and returns an int.

like image 578
Syfu_H Avatar asked Apr 16 '19 20:04

Syfu_H


People also ask

How do you call a function with a pointer argument?

To pass the value by pointer, argument pointers are passed to the functions just like any other value. So accordingly you need to declare the function parameters as pointer types as in the following function swap(), which exchanges the values of the two integer variables pointed to by its arguments.

CAN default arguments be provided for pointers to functions?

Default argument cannot be provided for pointers to functions.

Can a function pointer be used to call a function?

Function pointers in C can be used to create function calls to which they point. This allows programmers to pass them to functions as arguments. Such functions passed as an argument to other functions are also called callback functions.


1 Answers

Defaulted arguments are a bit of C++ syntactic sugar; when calling the function directly with insufficient arguments, the compiler inserts the default as if the caller had passed it explicitly, so the function is still called with the full complement of arguments (Mult(4) is compiled into the same code as Mult(4, 2) in this case).

The default isn't actually part of the function type though, so you can't use the default for an indirect call; the syntactic sugar breaks down there, since as soon as you are calling through a pointer, the information about the defaults is lost.

like image 59
ShadowRanger Avatar answered Oct 23 '22 06:10

ShadowRanger