Function Pointer to Member Function in C++ Last Updated : 28 May, 2024 Comments Improve Suggest changes Like Article Like Report In C++, function pointers enable users to treat functions as objects. They provide a way to pass functions as arguments to other functions. A function pointer to a member function is a pointer that points to a non-static member function of a class. In this article, we will learn how to use a function pointer to a member function in C++. Function Pointer to Member Function in C++In C++, functions can be treated as objects with the help of function pointers. A function pointer to a member function is a pointer that can refer to a member function of a specific class. Member functions differ from regular functions because they are associated with an instance of a class, which means they have an implicit this pointer that points to the object invoking the object. Following is the syntax to declare a function pointer to a member function: Syntaxreturn_type (ClassName::*pointer_name)(argument_types) = &ClassName::member_function;where: return_type: is the return type of the member function of the class.ClassName: is the name of the class to which the member function belongs.*pointer_name: is the name of the function pointer variable.argument_types: are the types of the arguments accepted by the member function.&ClassName::member_function: is the address of the member function being assigned to the function pointer.C++ Program to Declare a Function Pointer to Member Function The below example demonstrates how we can declare a function pointer to a member function in C++: C++ // C++ Program to Declare a Function Pointer to Member // Function #include <iostream> using namespace std; class MyClass { public: int value; // Constructor to initialize the member variable 'value' MyClass(int val) : value(val) { } // Member function to add two integers and return the // result int add(int x, int y) { return x + y; } }; int main() { // Create an instance of MyClass with the initial value // of 10 MyClass obj(10); // Declare a pointer to the member function 'add' of // MyClass int (MyClass::*ptrToMemberFunc)(int, int) = &MyClass::add; // Call the member function 'add' using the function // pointer int result = (obj.*ptrToMemberFunc)(20, 30); // Print the result of the function call cout << "Result from the member function: " << result << endl; return 0; } OutputResult from the member function: 50 Time Complexity: O(1) Auxiliary Space: O(1) Explanation In the above example, we have defined a class MyClass with a member function add that takes two integers and returns their sum.We have then declared function pointer ptrToMemberFunc that points to the member function add.To call the member function using the function pointer, we have used the syntax (obj.*ptrToMemberFunc)(20, 30) where obj is the instance of the class and *ptrToMemberFunc dereferences the function pointer to call the member function with the arguments 20 and 30 of type int. Comment More infoAdvertise with us Next Article Function Pointer to Member Function in C++ D denzirop9v Follow Improve Article Tags : C++ Programs C++ Misc C++ CPP-OOPs CPP Examples misc-cpp +2 More Practice Tags : CPP Similar Reads Function Overloading vs Function Templates in C++ In C++, both function overloading and function templates allow us to create functions that can operate on different types of data. While they might seem similar, they are used for different purposes. In this article, we will learn the differences between function overloading and function templates, 4 min read Returning a function pointer from a function in C/C++ In C/ C++, like normal data pointers(int *, char *, etc), there can be pointers to functions. Every function created in a program gets an address in memory since pointers can be used in C/C++, so a pointer to a function can also be created. Syntax: return type (*function_pointer_name) (argument_type 6 min read How to Create a Pointer to a Function in C++? In C++, a function pointer is a variable that stores the address of a function that can later be called through that function pointer. It is useful for passing functions as parameters to other functions(callback functions) or storing them in data structures. In this article, we will learn how to use 2 min read How to Return a Pointer from a Function in C++? In C++, we can return a pointer from a function which is useful when we want to return large data structures that cannot be returned by value. However, it must be done carefully to avoid memory leaks, dangling pointers, and other issues related to dynamic memory management. In this article, we will 2 min read How to Create a Function Template in C++? In C++, templates enable us to write generic programs that handle any data type. We can create a template class, function, and variable. A template function is a function that can work with any data type. In this article, we will learn how to create a function template in C++. Create a Function Temp 2 min read Like