Function Pointers and Inheritance

Can I assign a derived class member function to a base class function pointer? How does inheritance affect function pointers?

When dealing with inheritance and function pointers, there are a few things to consider. In general, you can assign a derived class member function to a base class function pointer, as long as the function signatures match. However, there are some caveats. Let's look at an example:

#include <iostream>

class Base {
 public:
  void foo() {
    std::cout << "Base::foo()\n"; }
};

class Derived : public Base {
 public:
  void foo() {
    std::cout << "Derived::foo()\n"; }
};

int main() {
  void (Base::*fooPtr)(){&Base::foo};

  Base* basePtr = new Derived;
  (basePtr->*fooPtr)();  

  Derived* derivedPtr = new Derived;
  (derivedPtr->*fooPtr)();  

  delete basePtr;
  delete derivedPtr;
}
Base::foo()
Base::foo()

In this example, fooPtr is a pointer to a member function of the Base class. We can assign the address of Base::foo() to it.

When we call the function pointer on a Base* that points to a Derived object, it invokes the Base::foo() implementation, not the Derived::foo() override. This is because the function pointer type is based on the Base class.

Similarly, even when we call the function pointer on a Derived*, it still invokes the Base::foo() implementation.

To achieve polymorphic behavior with function pointers, you need to use virtual functions. In the example above, if Base::foo() is declared as virtual and Derived::foo() overrides it, the behavior changes:

#include <iostream>

class Base {
 public:
  virtual void foo() {
    std::cout << "Base::foo()\n"; }
};

class Derived : public Base {
 public:
  void foo() override {
    std::cout << "Derived::foo()\n"; }
};

int main() {/*...*/}
Derived::foo()
Derived::foo()

Now, the function pointer invokes the derived class's implementation when called on a derived class object, exhibiting polymorphic behavior.

It's important to note that if the function signatures don't match between the base and derived classes, you won't be able to assign the derived class function to a base class function pointer.

Also, be cautious when using raw pointers and make sure to properly deallocate the memory to avoid leaks. In modern C++, it's recommended to use smart pointers or other resource management techniques to handle object lifetimes more safely.

Function Pointers

Learn about function pointers: what they are, how to declare them, and their use in making our code more flexible

Questions & Answers

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

Storing Function Pointers
How can I store multiple function pointers of the same type in a collection like a vector or array?
Pointers to Member Functions
Is it possible to create a pointer to a member function of a class? If so, how is it different from a regular function pointer?
Capturing this in Lambdas
How can I use a lambda to create a function pointer that calls a member function of my class?
Function Pointers and Const
How does const-correctness apply to function pointers? Can I have a pointer to a const function?
Using Function Pointers with STL Algorithms
Can I use function pointers with STL algorithms like std::find_if or std::sort? How can I pass a function pointer as a predicate or comparison function?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant