Yes, it is possible to use member function pointers with non-public (protected or private) member functions in C++. However, there are some important considerations and limitations to keep in mind. Let's explore this topic in detail:
In C++, you can create pointers to private and protected member functions. The key thing to remember is that the accessibility of a member function doesn't affect your ability to take its address. However, it does affect where you can use that pointer.
Let's start with a simple class that has private and protected methods:
#include <iostream>
class MyClass {
private:
void privateMethod() {
std::cout << "Private method\n";
}
protected:
void protectedMethod() {
std::cout << "Protected method\n";
}
public:
void publicMethod() {
std::cout << "Public method\n";
}
// Function to demonstrate usage
void demo();
};
You can create pointers to these methods like this:
void (MyClass::*privatePtr)() = &MyClass::privateMethod;
void (MyClass::*protectedPtr)() = &MyClass::protectedMethod;
void (MyClass::*publicPtr)() = &MyClass::publicMethod;
You can only create these pointers in contexts where you would normally be allowed to access the functions.
Here's how you might use these pointers inside a member function of MyClass
:
#include <iostream>
class MyClass {/*...*/};
void MyClass::demo() {
void (MyClass::*privatePtr)() =
&MyClass::privateMethod;
void (MyClass::*protectedPtr)() =
&MyClass::protectedMethod;
void (MyClass::*publicPtr)() =
&MyClass::publicMethod;
MyClass obj;
(obj.*privatePtr)();
(obj.*protectedPtr)();
(obj.*publicPtr)();
}
int main() {
MyClass Example;
Example.demo();
}
Private method
Protected method
Public method
You cannot create pointers to private or protected methods outside of the class or its derived classes. For example, this would not compile:
int main() {
MyClass obj;
// Error! Cannot access private member
void (MyClass::*ptr)() =
&MyClass::privateMethod;
}
error: 'MyClass::privateMethod': cannot access private member declared in class 'MyClass'
Friend classes and functions can use pointers to private and protected members, just as they can access these members directly:
#include <iostream>
class MyClass {
void privateMethod() {
std::cout << "Private method\n";
}
friend class FunctionCaller;
};
class FunctionCaller {
public:
void usePrivateMethod(MyClass& obj) {
// OK, FunctionCaller is a friend of MyClass
void (MyClass::*ptr)() =
&MyClass::privateMethod;
(obj.*ptr)();
}
};
int main() {
MyClass Object;
FunctionCaller Friend;
Friend.usePrivateMethod(Object);
}
Private method
Remember, the ability to create pointers to non-public members doesn't bypass C++'s access control mechanisms. It simply allows you to store and pass around the address of these functions in a type-safe way.
The actual invocation of these functions through the pointers is still subject to the normal access control rules.
Answers to questions are automatically generated and may not have been reviewed.
Explore advanced techniques for working with class member functions