Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

Deleted copying (`= delete`) and a defaulted virtual destructor (`= default`)

Disable copy construction and copy assignment separately, while letting the compiler define a virtual destructor for correct destruction through a base pointer.

Version: ISO C++23; implementation support may vary

Syntax

struct Base {
    Base() = default;
    Base(const Base&) = delete;
    Base& operator=(const Base&) = delete;
    virtual ~Base() = default;
};

“Deleted copying with = delete and defaulted virtual destructors with = default” names two separate choices. Base(const Base&) = delete; prevents constructing a new Base by copying, while Base& operator=(const Base&) = delete; prevents copy assignment to an existing object. Both functions still take part in overload resolution. Directly using either deleted function in the example makes the program ill-formed and requires a diagnostic. If substituting template arguments makes a dependent requirement invalid because it selects a deleted function, the requires-expression can instead evaluate to false without a diagnostic. = delete does not free memory.

In virtual ~Base() = default;, virtual enables virtual dispatch and = default asks the compiler to supply the usual destructor definition. It still destroys member and base subobjects; defaulted does not mean no work, nor is it the same as = 0. If a member has a deleted destructor, the class's defaulted destructor can itself be defined as deleted.

With ordinary single-object delete p, where Base* p points to a Derived created by new, virtual dispatch ensures that ~Derived() runs first and ~Base() follows. Without a virtual base destructor, this ordinary deletion would have undefined behavior. This rule does not make it safe to use delete[] on an array of derived objects through a base pointer.

The deleted copy constructor is still user-declared, so the example explicitly adds Base() = default;. The declared copy operations and destructor also prevent implicit declaration of Base's move constructor and move assignment operator. Deleting copies therefore does not make a type move-only by itself. The rules are described for ISO C++23; implementation support may vary.

Examples

#include <iostream>
#include <type_traits>

struct Base {
    Base() = default;
    Base(const Base&) = delete;
    Base& operator=(const Base&) = delete;
    virtual ~Base() = default;
};

struct Derived final : Base {
    ~Derived() override {
        std::cout << "Derived destroyed\n";
    }
};

static_assert(!std::is_copy_constructible_v<Base>);
static_assert(!std::is_copy_assignable_v<Base>);

int main() {
    Base* p = new Derived;
    delete p;

    // Base a, b;
    // Base c(a);
    // b = a;
}

Result

Derived destroyed

The `static_assert`s check that both copy operations are unavailable. `delete p` runs the `Derived` destructor, which prints `Derived destroyed`, then the `Base` destructor, which prints nothing. Uncomment `Base a, b;` and test `Base c(a);` and `b = a;` separately: each should fail to compile.

Related lessons

  • C++ · Lesson 55 · Intermediate · Version: 2

Sources