

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Function pointer to member function in C++
In C++ , function pointers when dealing with member functions of classes or structs, it is invoked using an object pointer or a this call. We can only call members of that class (or derivatives) using a pointer of that type as they are type safe.
Example
#include <iostream> using namespace std; class AB { public: int sub(int a, int b) { return a-b; } int div(int a, int b) { return a/b; } }; //using function pointer int res1(int m, int n, AB* obj, int(AB::*fp)(int,int)) { return (obj->*fp)(m,n); } //using function pointer int res2(int m, int n, AB* obj, int(AB::*fp2)(int,int)) { return (obj->*fp2)(m,n); } int main() { AB ob; cout << "Subtraction is = " << res1(8,5, &ob, &AB::sub) << endl; cout << "Division is = " << res2(4,2, &ob, &AB::div) << endl; return 0; }
Output
Subtraction is = 3 Division is = 2
- Related Questions & Answers
- Calling a member function on a NULL object pointer in C++
- Function Pointer in C
- How to declare member function in C# interface?
- Declare a C/C++ function returning pointer to array of integer function pointers
- How to declare a pointer to a function in C?
- How to assign a pointer to function using C program?
- Count the number of objects using Static member function in C++
- Count the number of objects using Static member function in C++ Program
- Double Pointer (Pointer to Pointer) in C
- How to define pointer to pointer in C language?
- Function returning another function in JavaScript
- Explain the concept of pointer to pointer and void pointer in C language?
- Member variables in Java
- How to call jQuery function with JavaScript function?
- How is Finance Function related to Management Function?
Advertisements