In C++, an Effect constructor calls the virtual reset() that Delay overrides; which reset() runs, and what changes if reset() is pure virtual?
answer
- the derived part does not exist yet
- dynamic type during construction
- same rule in the destructor
- pure: undefined, often an abort
- two-phase init or a factory
basics
~10 sEffect::reset runs, not Delay::reset: during Effect's constructor and destructor a virtual call resolves to the final overrider in Effect itself. If that function is pure, the call is undefined behaviour, often an abort.
solid answer
~50 sWhile `Effect`'s constructor runs, the `Delay` part of the object has not been built, so the standard says a virtual call made from a constructor or destructor on the object under construction calls the **final overrider in the constructor's or destructor's own class**, not one in a more-derived class. `reset()` therefore runs `Effect::reset`, and the same applies in `~Effect`, when `Delay`'s members are already destroyed. If `reset()` is **pure virtual**, that call is **undefined behaviour**. Clang warns about a direct call with `-Wcall-to-pure-virtual-from-ctor-dtor`, but a call hidden behind a non-virtual helper compiles silently; with libc++abi it aborted with `Pure virtual function called!`. The fix is to stop needing derived behaviour during base construction: pass the data to the base constructor, do the work in `Delay`'s constructor, or use a factory that constructs first and then calls a virtual `init()`.
code
cpp · 19 lines#include <cstdio>
#include <vector>
struct Effect {
Effect() { reset(); } // virtual call during construction
virtual ~Effect() { reset(); } // and during destruction
virtual void reset() { std::puts("Effect::reset"); }
};
struct Delay : Effect {
std::vector<float> line = std::vector<float>(4800);
Delay() { reset(); }
~Delay() override { std::puts("~Delay"); }
void reset() override { std::puts("Delay::reset"); }
};
int main() {
Delay d;
}go deeper
Remember the outcome: inside a base constructor or destructor, virtual calls run the base class's version, never the derived override.
Explain why: bases are constructed first and destroyed last, so the derived members do not exist yet or no longer exist when the base code runs.
Separate the defined case from the undefined one: a pure function reached from a constructor or destructor, warnings that miss indirect calls, and the same abort caused by destruction races.
Choose an initialisation design for a plugin hierarchy, constructor parameters or a factory with a virtual init(), that makes the half-built object unreachable.
## The rule The standard's section on construction and destruction says: when a virtual function is called directly or indirectly from a constructor or a destructor, and the object the call applies to is the object under construction or destruction, **the function called is the final overrider in the constructor's or destructor's class, not one overriding it in a more-derived class**. In the audio framework: ```cpp struct Effect { Effect() { reset(); } virtual ~Effect() { reset(); } virtual void reset(); }; struct Delay : Effect { std::vector<float> line = std::vector<float>(4800); Delay() { reset(); } void reset() override; // clears line }; ``` Constructing a `Delay` runs `Effect::reset` from `Effect()`, then `Delay::reset` from `Delay()`. Destroying it runs `~Delay`, then `Effect::reset` from `~Effect`. ## Why C++ chooses the base version Construction proceeds **base first**, then members, then the derived constructor body; destruction runs in reverse. While `Effect()` runs, `Delay::line` does not exist yet. If the call reached `Delay::reset`, that function would clear a vector whose constructor has not run, which would read uninitialised memory. C++ therefore treats the object, for the duration of each constructor and destructor, as if its dynamic type were the class whose constructor or destructor is running. The same view applies to `typeid` and `dynamic_cast` on the object during that time. Common implementations get this behaviour by storing the vtable pointer **per construction stage**: each constructor sets the object's vtable pointer to its own class's table before running its body, and each destructor restores its own class's table. That is an implementation technique, not a requirement; the requirement is only which function gets called. ## When the function is pure virtual ```cpp struct Effect { Effect() { init(); } void init() { reset(); } // indirect call virtual ~Effect() = default; virtual void reset() = 0; }; ``` The final overrider in `Effect` is the pure `Effect::reset`, and the standard says that making a virtual call to a pure virtual function from the abstract class's constructor or destructor is **undefined behaviour**. What you observe depends on the call: - **A direct call** in the constructor body is caught at compile time by Clang: `warning: call to pure virtual member function 'reset' has undefined behavior; overrides of 'reset' in subclasses are not available in the constructor of 'Effect'`. - **An indirect call** through a non-virtual helper, as above, compiles with no warning. Implementations that follow the Itanium C++ ABI fill a pure function's vtable slot with a runtime error handler; with libc++abi the run printed `libc++abi: Pure virtual function called!` and aborted. - With **multiple threads**, the same message can appear when one thread calls a virtual function on an object that another thread is destroying, because the object's dynamic type has already moved back to the base. That is a data race and a lifetime bug in its own right. ## Fixes 1. **Pass data down, not behaviour up.** If the base needs a value that the derived class knows, take it as a constructor parameter: `Delay() : Effect(kDelayLatency) {}`. 2. **Do derived work in the derived constructor.** `Delay()` can call `reset()` itself; at that point the dynamic type is `Delay`. 3. **Two-phase initialisation behind a factory.** Construct completely, then call a virtual `init()`: ```cpp template <class T, class... Args> std::unique_ptr<Effect> makeEffect(Args&&... args) { auto fx = std::make_unique<T>(std::forward<Args>(args)...); fx->init(); // full dynamic type now return fx; } ``` Make constructors `protected` so callers cannot skip the factory. 4. **Destructors:** release derived resources in the derived destructor; never rely on a virtual call in a base destructor to clean up derived state. 5. **Callbacks registered from a base constructor:** if the base hands `this` to an audio thread or an event system before the derived constructor has run, that system can call a virtual function on a half-built object. Register only after construction completes, again from a factory. Making the constructor itself virtual is not an option: the compiler rejects it with `error: constructor cannot be declared 'virtual'`. ## Quick reference | Call made from `Effect`'s constructor or destructor | Result | |---|---| | virtual `reset()` that `Effect` defines | `Effect::reset` runs | | virtual `reset()` that is pure in `Effect` | undefined behaviour; often an abort | | non-virtual helper that calls a pure `reset()` | undefined behaviour, usually no warning | | `reset()` from `Delay`'s constructor | `Delay::reset` runs |
- Does the same rule apply in a destructor, and why does that matter for cleanup?Yes. Once `~Delay` has finished, the object is treated as an `Effect` again, so a virtual call from `~Effect` runs `Effect`'s version. Cleanup of derived resources must therefore live in the derived destructor; a base destructor that calls a virtual `release()` expecting the derived override will run the base version instead, or hit undefined behaviour if it is pure.
- How can a pure-virtual-call abort appear in a program that never calls virtual functions from constructors?When one thread calls a virtual function on an object while another thread is destroying it. After the derived destructor finishes, the object's dynamic type is the base again, so the racing call can land on a pure function. The abort message is then a symptom of a data race and a lifetime bug, not of constructor code.
- The base constructor calls Delay::reset because the object being built is a Delay.
- Calling any virtual function from a constructor is undefined behaviour.
- Calling a pure virtual function from a constructor is a guaranteed compile error.
- Wrapping the call in a non-virtual helper makes it dispatch to the derived class.
- You can fix it by declaring the constructor virtual.