Member Function Pointers and Binding

Using Pointers to Non-Public Member Functions

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

Abstract art representing computer programming

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.

Answers to questions are automatically generated and may not have been reviewed.

sdl2-promo.jpg
Part of the course:

Game Dev with SDL2

Learn C++ and SDL development by creating hands on, practical projects inspired by classic retro games

Free, unlimited access

This course includes:

  • 67 Lessons
  • 100+ Code Samples
  • 91% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved