How to pick the correct error() member function? Do I need to cast somehow?
using namespace std; struct test { int error(); void error(int x); int fun(); }; int main() { auto f1 = &test::error; // how to pick the correct function? auto f2 = &test::fun; // works }
Overloading Member Functions in C++C++ enables several functions of the same name to be defined, as long as they have different signatures. This is called function overloading. The C++ compiler selects the proper function to call by examining the number, types and order of the arguments in the call.
You can overload both member functions and free functions. The following table shows which parts of a function declaration C++ uses to differentiate between groups of functions with the same name in the same scope.
And C doesn't support Function Overloading.
Function overloading is a C++ programming feature that allows us to have more than one function having same name but different parameter list, when I say parameter list, it means the data type and sequence of the parameters, for example the parameters list of a function myfuncn(int a, float b) is (int, float) which is ...
You can just explicitly specify the member function pointer type.
int (test::*f1)() = &test::error; void (test::*f2)(int) = &test::error;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With