Function Pointers

Function Pointers and Inheritance

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

Abstract art representing computer programming

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.

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

A computer programmer
Part of the course:

Professional C++

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

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% 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