Infinite loop in a C++ constructor

I've got this class in c++ and I'm having trouble with the second constructor: for some unknown reason, the IDE is acusing an infinite loop. I don't have any idea what is causing it, since I've written similar loops countless times. The code in question is this:

class ArithmeticProgression : protected Progression{

    //Constructors
    explicit ArithmeticProgression(double reason) : Progression(){
        this->reason = reason;
    }

    ArithmeticProgression(double reason, int maxIndex) : Progression(){
        this->reason = reason;
        for(int i = 0; i < maxIndex; i++){
            
        }
    }

    //Destructor
    ~ArithmeticProgression(){
        delete this;
    }

protected:
    double reason;
};

1 answer

  • answered 2022-05-07 01:24 Blindy

    You must be reading it wrong, because the infinite loop is in your destructor:

    ~ArithmeticProgression(){
        delete this;
    }
    

    You keep invoking it from within itself for absolutely no sane reason.

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum