Member Function Pointers and Binding

Virtual Member Function Pointers

Is it possible to create a pointer to a virtual member function? What are the implications?

Abstract art representing computer programming

Yes, it is possible to create a pointer to a virtual member function in C++. However, there are important implications and behaviors to understand.

Basic Syntax

Creating a pointer to a virtual function uses the same syntax as for non-virtual functions:

#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() {
  void (Base::*ptrToVirtual)() = &Base::foo;

  Base b;
  Derived d;

  (b.*ptrToVirtual)();
  (d.*ptrToVirtual)();
}
Base::foo()
Derived::foo()

Late Binding Is Preserved

The key implication is that virtual function pointers still preserve late binding (dynamic dispatch).

When you call a virtual function through a pointer-to-member, the actual function called is determined by the runtime type of the object, not the type of the pointer.

Base Class Pointers

You can store a pointer to a derived class's virtual function in a base class function pointer:

void (Base::*ptrToDerivedVirtual)() =
  &Derived::foo;
Base* bp = new Derived();

// Outputs: Derived::foo()
(bp->*ptrToDerivedVirtual)();

std::mem_fn() and std::function

These utilities work well with virtual functions and preserve their polymorphic behavior:

auto virtualFn = std::mem_fn(&Base::foo);
Base b;
Derived d;
virtualFn(b); // Outputs: Base::foo()
virtualFn(d); // Outputs: Derived::foo()

Performance Considerations

Using virtual function pointers doesn't introduce additional runtime overhead compared to normal virtual function calls. The vtable lookup still occurs.

Multiple Inheritance

With multiple inheritance, you need to be careful about which base class's virtual function you're pointing to:

#include <iostream>

class A {
 public:
  virtual void foo() {
    std::cout << "A::foo()\n";
  }
};
class B {
 public:
  virtual void foo() {
    std::cout << "B::foo()\n";
  }
};
class C : public A, public B {
 public:
  void foo() override {
    std::cout << "C::foo()\n";
  }
};

int main() {
  void (A::*ptrA)() = &A::foo;
  void (B::*ptrB)() = &B::foo;

  C c;
  (c.*ptrA)();  // Outputs: C::foo()
  (c.*ptrB)();  // Outputs: C::foo()
}
C::foo()
C::foo()

Pure Virtual Functions

You can create pointers to pure virtual functions, but attempting to call them through a base class object will result in undefined behavior:

#include <iostream>

class Abstract {
 public:
  virtual void pureVirtual() = 0;
};

class Concrete : public Abstract {
 public:
  void pureVirtual() override {
    std::cout << "Concrete::pureVirtual()\n";
  }
};

int main() {
  void (Abstract::*ptrToPureVirtual)() =
    &Abstract::pureVirtual;

  Concrete c;
  (c.*ptrToPureVirtual)();

  // Error: cannot instantiate abstract class
  // Abstract a; 

  // Undefined behavior if a existed
  // (a.*ptrToPureVirtual)(); 
}
Concrete::pureVirtual()

Type Safety

While virtual function pointers preserve polymorphism, they don't provide additional type safety. It's the programmer's responsibility to ensure that the object used with the function pointer is of a compatible type.

Understanding these implications allows you to leverage virtual function pointers effectively in C++, particularly in scenarios involving plugin architectures, callback systems, or when implementing generic algorithms that need to work with polymorphic types.

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:

  • 55 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