The Array new and delete Operators - C++ Operator

C++ examples for Operator:new

Description

The Array new and delete Operators

Demo Code

#include <iostream>

 using namespace std;

 class MyClass//w  ww  .  ja v a 2  s  . c  o  m
 {
 private:
    int m_Number{ 0 };

 public:
    MyClass() = default;
    ~MyClass()
    {
        cout << "Destroying " << m_Number << endl;
    }

    void operator=(const int value)
    {
        m_Number = value;
    }
 };

 int main(int argc, char* argv[])
 {
    const unsigned int NUM_ELEMENTS{ 5 };
    MyClass* pObjects{ new MyClass[NUM_ELEMENTS] };
    pObjects[0] = 100;
    pObjects[1] = 45;
    pObjects[2] = 31;
    pObjects[3] = 90;
    pObjects[4] = 58;

    delete[] pObjects;
    pObjects = nullptr;

    return 0;
}

Result


Related Tutorials