C++

Pure Virtual Method Called Fix

While a base class constructor or destructor is running, the derived part of the object does not exist yet, or no longer exists, so a virtual call resolves to the base class version. When that version is pure virtual, there is no function to run: move the call out of the constructor and destructor, into a step that runs on the finished object.

Written November 26, 2026 · Tested with GCC 13.3.0 · 11 min read

By the end of this guide
  • The error: a constructor reaches a pure virtual function through a helper
  • The fix: call the virtual function after construction completes
  • The destructor shape: the same message while the object is taken apart
  • A neighbor that fails at build time: the direct call

Written November 26, 2026

Tested with GCC 13.3.0

What this guide is

This message is a virtual function call landing on a function that does not exist. A base class declares speak() pure virtual, so the base class itself supplies no implementation. If a base class constructor or destructor reaches that function while the object is still being built, or is already being taken apart, the call cannot reach the derived override: during construction and destruction, a virtual call resolves to the final overrider in the class whose constructor or destructor is running, not to an override in a more-derived class. For a pure virtual function, that resolution has nowhere to go, and the runtime stops the program instead.

The shape that produces the message in real code is rarely a direct call. It is an ordinary helper: the constructor calls a non-virtual member function such as init(), and init() calls the virtual function. That indirection is also why the failure arrives at run time rather than at build time. The compiler cannot see a direct call to a pure function, so it emits a normal virtual call; the program compiles, links, starts, and aborts the first time the constructor runs.

The same bug has a second face, and the difference is diagnostic. When the constructor calls the pure virtual function directly, GCC warns at compile time and the link then fails with an undefined reference to the pure function, because the direct call is emitted as a call to a function that has no definition. Same defect, two failure surfaces: an indirect call fails at run time with this guide's message, a direct call fails at build time with a linker error. Which surface a report describes tells you how the call is written before you open the file.

The fix is structural, not a repair to the call itself. No call to a virtual function belongs in a constructor or a destructor at all. Work that needs the derived behavior moves to a step that runs after construction completes — a run() or init() step called on the finished object — and cleanup that needs the derived behavior runs before destruction begins, through an explicit close() call while the object is fully alive. Both fixed shapes are shown below with real output.

The examples here use GCC 13.3.0 with C++20 sources. Each aborting program prints the two runtime lines to standard error and exits with status 134; each fixed program prints derived and exits with status 0.

The error: a constructor reaches a pure virtual function through a helper

The smallest shape is a base constructor that calls a non-virtual member function, which in turn calls the pure virtual function:

C++
#include <iostream>

struct Base {
    Base() { init(); }
    void init() { speak(); }
    virtual void speak() = 0;
    virtual ~Base() = default;
};

struct Derived : Base {
    void speak() override { std::cout << "derived\n"; }
};

int main() {
    Derived d;
    return 0;
}

Nothing in this program looks wrong at the call site in main: a Derived object is created, and Derived does override speak(). The defect is in the order of events. Base's constructor runs before the Derived part of the object exists, so when init() makes its virtual call, the call resolves to Base's version — the pure one. The program compiles with no diagnostic and then aborts as soon as it runs:

Terminal
$ g++ -std=c++20 indirect_ctor.cpp -o indirect_ctor
$ ./indirect_ctor
pure virtual method called
terminate called without an active exception

The exit status is 134, the shell's report of a program ended by SIGABRT. Read the two lines in order. pure virtual method called is the runtime's report of where the virtual call landed: the entry for a pure virtual function in the class's virtual table points at a runtime error handler, __cxa_pure_virtual, whose job is to print that line and end the program. terminate called without an active exception follows because that handler calls std::terminate, and no exception is in flight. Neither line names a file or a line number, which is why the search has to start from the structure of the code — a constructor or destructor that can reach a virtual call — rather than from the message text.

The fix: call the virtual function after construction completes

Remove the call from the constructor and give the class a step that runs on the finished object. Here the base constructor does nothing, and a public run() member makes the same call init() made before. By the time main calls run(), the Derived part exists, so the virtual call dispatches normally:

C++
#include <iostream>

struct Base {
    Base() = default;
    void run() { speak(); }
    virtual void speak() = 0;
    virtual ~Base() = default;
};

struct Derived : Base {
    void speak() override { std::cout << "derived\n"; }
};

int main() {
    Derived d;
    d.run();
    return 0;
}
Terminal
$ g++ -std=c++20 fixed.cpp -o fixed
$ ./fixed
derived

The program prints derived and exits with status 0. The call itself is unchanged — run() calls speak() exactly as init() did — and only its timing changed: it now happens after construction completes, on an object whose derived part is alive. In a real class this step is the one the object's owner calls once the object exists: a factory returns the object and then calls the step, or main calls it, or a framework's post-construction hook calls it. What it cannot be is the constructor, because the constructor is the one moment when the call is guaranteed to resolve to the wrong class.

The destructor shape: the same message while the object is taken apart

Destruction is construction in reverse, and the same rule applies from the other end. Here the base destructor calls the same helper, and the helper calls the pure virtual function. The object is created with new Derived, held in a Derived*, and deleted through that pointer; by the time Base's destructor runs, the Derived part has already been destroyed:

C++
#include <iostream>

struct Base {
    Base() = default;
    ~Base() { init(); }
    void init() { speak(); }
    virtual void speak() = 0;
};

struct Derived : Base {
    void speak() override { std::cout << "derived\n"; }
};

int main() {
    Derived* p = new Derived;
    delete p;
    return 0;
}
Terminal
$ g++ -std=c++20 indirect_dtor.cpp -o indirect_dtor
$ ./indirect_dtor
pure virtual method called
terminate called without an active exception

The output and the exit status, 134, are identical to the constructor shape, which is the point: the message alone does not say whether the call came from construction or destruction, and both places have to be searched. A program that constructs cleanly and aborts on exit, at shutdown, or when a container is cleared is usually showing this second shape.

The fix mirrors the constructor fix. The destructor does not call the virtual function; cleanup that needs the derived behavior runs while the object is fully alive, through an explicit close() call, and the destructor is left with the base class's own resources only:

C++
#include <iostream>

struct Base {
    Base() = default;
    void close() { speak(); }
    virtual void speak() = 0;
    virtual ~Base() = default;
};

struct Derived : Base {
    void speak() override { std::cout << "derived\n"; }
};

int main() {
    Derived* p = new Derived;
    p->close();
    delete p;
    return 0;
}
Terminal
$ g++ -std=c++20 fixed_dtor.cpp -o fixed_dtor
$ ./fixed_dtor
derived

The program prints derived and exits with status 0. close() runs before delete, while the whole object still exists, so its virtual call reaches Derived::speak(). The destructor that follows has no virtual call in it, so the order in which the parts are destroyed no longer matters to it.

A neighbor that fails at build time: the direct call

Write the same defect without the helper — the constructor calls the pure virtual function directly — and the failure moves from run time to build time. The compiler can see this call, and it warns; the link then fails:

C++
#include <iostream>

struct Base {
    Base() { speak(); }
    virtual void speak() = 0;
    virtual ~Base() = default;
};

struct Derived : Base {
    void speak() override { std::cout << "derived\n"; }
};

int main() {
    Derived d;
    return 0;
}
Terminal
$ g++ -std=c++20 -c direct_ctor.cpp -o direct_ctor.o
direct_ctor.cpp: In constructor 'Base::Base()':
direct_ctor.cpp:4:19: warning: pure virtual 'virtual void Base::speak()' called from constructor
    4 |     Base() { speak(); }
      |              ~~~~~^~
$ g++ direct_ctor.o -o direct_ctor
/usr/bin/ld: direct_ctor.o: in function `Base::Base()':
direct_ctor.cpp:(.text._ZN4BaseC2Ev[_ZN4BaseC5Ev]+0x26): undefined reference to `Base::speak()'
collect2: error: ld returned 1 exit status

The warning names the defect exactly: a pure virtual function called from a constructor. The link error follows from how a direct call is compiled. A direct call is emitted as a call to Base::speak() itself, not as a virtual dispatch, and a pure virtual function normally has no definition for the linker to find — so the link fails with an undefined reference, and the compile driver reports that ld returned exit status 1. This is the easier of the two surfaces to diagnose, because it never reaches a user: the program does not build. It is also the rarer one in practice, because the direct call is the form a reader spots in review. The indirect form, through a helper with an ordinary name, is the one that ships.

A probe: giving the pure virtual function a definition

A pure virtual function is allowed to have a definition, provided out of line. Adding one to these shapes changes the two surfaces differently, and neither change is a fix.

For the direct call, the definition gives the linker the symbol it was missing. The compile still warns, the link now succeeds, and the program runs — printing the base definition, not the derived override, because the direct call in the constructor names the base function:

C++
#include <iostream>

struct Base {
    Base() { speak(); }
    virtual void speak() = 0;
    virtual ~Base() = default;
};

void Base::speak() { std::cout << "base definition\n"; }

struct Derived : Base {
    void speak() override { std::cout << "derived\n"; }
};

int main() {
    Derived d;
    return 0;
}
Terminal
$ g++ -std=c++20 -c defined_direct.cpp -o defined_direct.o
defined_direct.cpp: In constructor 'Base::Base()':
defined_direct.cpp:4:19: warning: pure virtual 'virtual void Base::speak()' called from constructor
    4 |     Base() { speak(); }
      |              ~~~~~^~
$ g++ defined_direct.o -o defined_direct
$ ./defined_direct
base definition

For the indirect call, the definition changes nothing. The helper's call is a virtual dispatch, and during construction the dispatch still resolves through the base class's virtual table, whose entry for the pure function is the runtime error handler — not the definition, which a virtual call never reaches. With the same definition added to the first program in this guide, the run is unchanged:

Terminal
$ g++ -std=c++20 defined_indirect.cpp -o defined_indirect
$ ./defined_indirect
pure virtual method called
terminate called without an active exception

The exit status is again 134. So the definition is not a repair in either shape: in the direct shape it turns a build failure into a program that silently runs base behavior where derived behavior was expected, and in the indirect shape the abort remains. Making a virtual call to a pure virtual function from a constructor or destructor is undefined behavior whether or not the function has a definition; the definition exists for a different purpose, letting derived implementations call the base version explicitly with a qualified name once the object is fully constructed.

The exact rule: during construction and destruction, the more-derived classes do not exist

When a virtual function is called directly or indirectly from a constructor or a destructor, the function called is the final overrider in the constructor's or destructor's class, and not one overriding it in a more-derived class. In other words, during construction or destruction, the more-derived classes do not exist. That rule covers calls made from member initializer lists and during the construction or destruction of non-static data members as well as calls in the constructor or destructor body, and it applies however many non-virtual member functions sit between the constructor and the virtual call — which is why the helper shape in this guide behaves exactly like the direct shape at the point of dispatch, and differently only in what the compiler can see and warn about.

A pure virtual function is declared with = 0 and makes its class abstract: no objects of that class can be created except as base subobjects of a derived class. Its definition may be provided out of line, and must be provided if the pure virtual function is the destructor, but making a virtual call to it from the constructor or destructor of the abstract class is undefined behavior regardless of whether it has a definition. In the implementation shown here, the undefined behavior takes a specific, repeatable form: the virtual table entry for a pure virtual function holds __cxa_pure_virtual, the runtime error handler, and reaching it prints pure virtual method called and terminates the program. A direct call bypasses the table, is compiled as a call to the function itself, and fails at link time when no definition exists. Neither surface is the derived override, and no form of the call from a constructor or destructor can reach it, because at that moment the derived part of the object is not there to be reached.

The fixes, ranked

  • First, remove every virtual call from constructors and destructors. Search the constructor and destructor bodies, then follow every non-virtual member function they call — init(), setup(), cleanup(), and their kin — because the indirect shape is the one that reaches run time.
  • Second, move work that needs the derived behavior to a post-construction step: a member function the owner calls on the finished object, a factory that calls it before handing the object out, or a framework hook that runs after construction.
  • Third, move cleanup that needs the derived behavior to an explicit call that runs before destruction — a close() or shutdown step the owner calls while the object is fully alive — and leave the destructor with the base class's own resources only.
  • Fourth, treat a definition for a pure virtual function as documentation of base behavior for derived classes to call explicitly, never as a repair: it does not change the indirect abort, and in the direct shape it replaces a build failure with silently wrong behavior.
  • Fifth, when a non-pure virtual function is called from a constructor or destructor and appears to work, fix it the same way: it runs the base version, not the override the code appears to call, and the base version's behavior is rarely what the call site expects.

Which fix for which cause

Cause Message Fix
Base constructor calls a helper that calls a pure virtual function pure virtual method called, then terminate called without an active exception, exit status 134 at startup Remove the call chain from the constructor; run the work in a post-construction step on the finished object
Base destructor calls a helper that calls a pure virtual function Identical lines and exit status 134, at deletion or shutdown Remove the call chain from the destructor; run the cleanup through an explicit call before destruction
Base constructor or destructor calls the pure virtual function directly, no definition Compiler warning pure virtual ... called from constructor (or ... called from destructor), then undefined reference to 'Base::speak()' and link exit status 1 Same structural fix; the build-time failure is the same defect seen earlier
Pure virtual function given a definition, indirect call from a constructor Identical runtime lines and exit status 134 Definition does not repair a virtual call; move the call out of the constructor or destructor
Pure virtual function given a definition, direct call from a constructor Warning at compile time, link succeeds, program runs the base definition Same structural fix; the program now builds but runs base behavior instead of the derived override
Non-pure virtual function called from a constructor or destructor No error; the base version runs silently Move the call to a post-construction or pre-destruction step so the intended override runs
## Checklist
  • Read the failure surface first: a run-time abort with pure virtual method called means the call is indirect; a warning plus an undefined reference at link time means the call is direct.
  • Search constructors and destructors for virtual calls, then follow every helper they call — the helper's name gives no sign that a virtual call is inside it.
  • Check member initializer lists and member construction as well as constructor bodies; the dispatch rule covers those calls too.
  • Never call a virtual function from a constructor or destructor, pure or not: the call resolves to the class currently being constructed or destroyed, never to the more-derived override.
  • Give work that needs derived behavior a named step after construction, and call it from the owner, the factory, or the framework hook — not from the constructor.
  • Give cleanup that needs derived behavior an explicit call before destruction, and keep destructors to their own class's resources.
  • Do not add a definition to a pure virtual function as a fix for this message: the indirect abort is unchanged, and the direct shape becomes a silent wrong-behavior bug.
  • After the fix, confirm the derived override is the function that runs: the fixed programs here print derived, where the broken ones abort and the defined direct shape prints the base definition.

Sources

  • cppreference, Virtual function specifier — for virtual functions supporting dynamic dispatch to the overriding behavior in a derived class, and for the rule that when a virtual function is called directly or indirectly from a constructor or a destructor, the function called is the final overrider in the constructor's or destructor's class and not one overriding it in a more-derived class — in other words, during construction or destruction, the more-derived classes do not exist.
  • cppreference, Abstract class — for a pure virtual function being a virtual function declared with = 0, for an abstract class being unusable except as a base class, for the definition of a pure virtual function being providable out of line and required for a pure virtual destructor, and for making a virtual call to a pure virtual function from a constructor or the destructor of the abstract class being undefined behavior regardless of whether it has a definition.
  • Linux Foundation, C++ Class Representations — for the virtual table of a class holding __cxa_pure_virtual() as the entry for a pure virtual function, supplementing the Itanium C++ ABI's virtual table specification.

Keep Exploring