samedi 18 décembre 2021

C++ Pointers and Destructors

class Dummy
{
public: 
    int* A{};
    int num{};
public:
    Dummy(int num)
    {
        this->num = num;
        A = new int[num];
    }
    ~Dummy()
    {
        delete[] A;
    }
};

Dummy* dummy()
{
    Dummy* d = new Dummy{ 4 };
    d->A[0] = 1;
    d->A[1] = 2;
    d->A[2] = 3;
    d->A[3] = 4;
    return d;
}
int main()
{
  Dummy* ATT = dummy();
}

When I tired to run this program This is always showing Expection at destructor and program can't continue further. What's wrong in this Code...

Aucun commentaire:

Enregistrer un commentaire