Using Pointers to Non-Public Member Functions

Can we use member function pointers with non-public (protected or private) member functions? If so, how?

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:

Basic Principle

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.

Creating Pointers to Non-Public Members

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;

Limitations

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

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

Use Cases and Best Practices

  1. Testing: Pointers to non-public members can be useful in unit testing, allowing test code to access and verify the behavior of private methods.
  2. Callbacks: You might use pointers to protected methods as callbacks in a base class, allowing derived classes to override the behavior.
  3. Design Patterns: Some design patterns, like the Template Method pattern, might use pointers to protected methods to customize behavior in derived classes.
  4. Caution: While it's possible to use pointers to non-public members, it's generally best to respect the encapsulation principles of OOP. If you find yourself frequently needing to access private members, consider if your class design could be improved.

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.

Member Function Pointers and Binding

Explore advanced techniques for working with class member functions

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Why Use Member Function Pointers?
Why do we need member function pointers when we can just call methods directly?
std::mem_fn() and Code Readability
How does using std::mem_fn() improve code readability compared to raw function pointers?
Const Member Function Pointers
How can we handle member function pointers for const member functions?
Virtual Member Function Pointers
Is it possible to create a pointer to a virtual member function? What are the implications?
Function Pointers with Abstract Base Classes
Can we use member function pointers with abstract base classes and derived classes?
Templated Member Function Pointers
How can we use member function pointers with templated member functions?
Function Pointers with Default Arguments
Can we use member function pointers with member functions that have default arguments?
Member Function Pointers and Multiple Inheritance
How do we work with member function pointers in the context of multiple inheritance?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant