Function Pointers and Virtual Functions

How do function pointers interact with virtual functions in C++? Can I take a pointer to a virtual function?

In C++, you can take a pointer to a virtual function, but it works a bit differently than taking a pointer to a non-virtual function.

When you take a pointer to a non-virtual member function, you get a normal function pointer that you can call directly:

class Base {
 public:
  void f() {}
};

int main() {
  void (Base::*fp)() = &Base::f;
  Base b;
  (b.*fp)();  
}

However, when you take a pointer to a virtual function, you get a pointer to the virtual function table (vtable) entry for that function, not a direct function pointer. To call it, you need an object instance:

class Base {
 public:
  virtual void f() {}
};

int main() {
  void (Base::*fp)() = &Base::f;
  Base b;
  (b.*fp)();
}

If you try to call the function pointer without an object instance, you'll get a compilation error.

The reason for this difference is that virtual functions are resolved dynamically based on the actual type of the object, which is not known at compile time. The vtable mechanism is used to achieve this dynamic dispatch.

When you call a virtual function through a pointer or reference to the base class, the compiler generates code to look up the function address in the vtable of the actual object at runtime. This allows the correct derived class function to be called even if the caller only knows about the base class.

So while you can take a pointer to a virtual function, it's not a regular function pointer and requires an object instance to be called. In most cases, it's simpler and safer to call virtual functions directly on objects or through pointers/references to base classes and let the language handle the dynamic dispatch.

First Class Functions

Learn about first-class functions in C++: a feature that lets you store functions in variables, pass them to other functions, and return them, opening up new design possibilities

Questions & Answers

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

Returning Function Pointers from Functions
How can I return a function pointer from a function in C++? What are some use cases for doing this?
Capturing Local Variables in C++ Lambdas
How can I capture local variables from the enclosing scope when using lambdas in C++? What are the different capture modes and when should I use each?
Downcasting Function Pointers in C++
Is it possible to downcast a function pointer to a more specific function pointer type in C++? For example, casting a pointer-to-base to a pointer-to-derived. If so, how is it done and what are the risks?
Understanding Complex Function Pointer Syntax
C++ function pointer syntax can get quite complex, especially with things like typedef and using directives. How can I decipher complex function pointer syntax?
Function Pointers and Static Member Functions
Can I take a function pointer to a static member function in C++? How is it different from taking a pointer to a non-static member function?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant