Yes, it is possible to create a pointer to a virtual member function in C++. However, there are important implications and behaviors to understand.
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()
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.
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()
Using virtual function pointers doesn't introduce additional runtime overhead compared to normal virtual function calls. The vtable lookup still occurs.
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()
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()
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.
Explore advanced techniques for working with class member functions