Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ member function overloading with & (ampersand)

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 } 
like image 885
Matthias Dieter Wallnöfer Avatar asked Mar 14 '16 10:03

Matthias Dieter Wallnöfer


People also ask

Can member function be overloaded?

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.

Can we overload a member function in C++?

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.

Is there function overloading in C?

And C doesn't support Function Overloading.

What is function overloading in C with example?

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 ...


1 Answers

You can just explicitly specify the member function pointer type.

int (test::*f1)() = &test::error; void (test::*f2)(int) = &test::error; 
like image 141
Joseph Thomson Avatar answered Sep 18 '22 17:09

Joseph Thomson