example2_use_with_array.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

class MyClass {
public:
    MyClass() {
        std::cout << "Constructor called." << std::endl;
    }
    ~MyClass() {
        std::cout << "Destructor called." << std::endl;
    }
};

int main() {
    MyClass* ptr = new MyClass[3];  // Allocate memory for an array of 3 MyClass objects

    delete[] ptr;  // Deallocate the memory for the entire array and call destructors

    return 0;
}
Back to delete