Pure virtual functions must not be called from a C++ destructor.
This error indicates that a destructor contains a call to a pure virtual function in that class. As a general rule, you should never call any kind of virtual function in a constructor or a destructor because those calls will never go to a more derived class than the currently executing constructor or destructor. In this case, the function being called is a pure virtual function, meaning that no implementation is provided in the class itself. Since it has no implementation, this call cannot be resolved and the program will fail.
It might seem that the call would be resolved to virtual function definition corresponding to the derived class that is being destroyed. The reason this cannot be done is because the derived class has already been torn down. The base class destructor is called after the destructor of the derived class.
ID |
Observation |
Description |
---|---|---|
1 |
Call site |
This shows where the virtual function was called |
2 |
Definition |
This shows where the destructor was defined |
#include <stdio.h> class A { public: A() { } void intermediate_call() { // Bad: virtual function call during object destruction virtual_function(); } virtual void virtual_function() = 0; virtual ~A() { intermediate_call(); } }; class B : public A { public: // override virtual function in A void virtual_function() { printf("B::virtual_function called\n"); } }; int main(int argc, char **argv) { B myObject; // This call behaves like a normal virtual function call. // Print statement shows it invokes B::virtual_function. myObject.virtual_function(); // Call to virtual_function during destruction doesn't // behave like normal virtual function call. // Tries to invoke A::virtual_function even though we // are destroying an instance of B, not A. Since this // doesn't exist the program fails catastrophically. }
Copyright © 2010, Intel Corporation. All rights reserved.