Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

Object model and composition in C++

An object has a type, storage, and a lifetime; it can contain member and base-class subobjects. Composition connects objects through members, while inheritance enables a shared interface. The language determines initialization and destruction order.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

struct Base { virtual ~Base() = default; virtual int value() const = 0; };
struct Part { int n; };
struct Whole : Base {
    Part part;
    explicit Whole(int n) : part{n} {}
    ~Whole() override = default;
    int value() const override { return part.n; }
};
// Explicit destructor call: p->~Whole();

An object occupies storage and has a lifetime. A complete object is not a subobject of another object. In the example, Whole contains a Base base-class subobject and a Part part member. The member represents composition (“has a part”), while the base represents inheritance. Merely holding a pointer to another object does not make that object a subobject of the pointer's owner.

A member initializer list, such as : part{n}, initializes the member before the constructor body runs; it is not assignment. Virtual bases are initialized first, and only by the constructor of the most-derived class. Next come the direct non-virtual bases and then the members in declaration order. The constructor body runs last. Reordering entries in the list does not change that order.

A destructor declaration has the form ~Part();; a destructor takes no arguments and has no return type. After its body, members and bases are destroyed in the reverse order of completion of their construction. A virtual destructor, such as virtual ~Base(), enables correct destruction of a derived object through delete on a base pointer. With ordinary deallocation, lacking such a destructor causes undefined behavior.

A pure virtual function, virtual int value() const = 0;, uses the = 0 pure-specifier, not assignment. Base is abstract: complete objects of that class cannot be created. Whole::value() supplies the implementation. A destructor can also be pure virtual, but still requires a definition when derived objects are created.

An Explicit destructor call, such as p->~Whole();, ends the object's lifetime but does not itself release storage. Do not use it on an ordinary local object: subsequent automatic destruction without correctly recreating the object causes undefined behavior. The example uses automatic destruction. The scope is C++23; the supplied standard URL hosts an evolving draft, not frozen C++23 text. A repository compiler flag does not attest the production runtime.

Examples

#include <iostream>

struct Part {
    int n;
    explicit Part(int x) : n{x} { std::cout << "Part+\n"; }
    ~Part() { std::cout << "Part-\n"; }
};

struct Base {
    virtual ~Base() { std::cout << "Base-\n"; }
    virtual int value() const = 0;
};

struct Whole : Base {
    Part part;
    explicit Whole(int n) : part{n} { std::cout << "Whole+\n"; }
    ~Whole() override { std::cout << "Whole-\n"; }
    int value() const override { return part.n; }
};

int main() {
    Whole object{7};
    const Base& view = object;
    std::cout << view.value() << '\n';
}

Result

Part+
Whole+
7
Whole-
Part-
Base-

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources