The program starts, but the Eclipse debugger hangs

The program is similar to this:

class A {
    const A& a;
public:
    A(const A& a) : a(a) {}
};

int main(int argc, char** argv) {

    A a(a);

}

The program compiles and runs. However, sometimes the Eclipse debugger freezes. Commenting out the line A a (a), fixing the problem.

Is this something from the dangerous line A a (a)?

+4
source share
1 answer

This is infinite recursion. The class constructor seems to be calling itself over and over again. if you are using gcc, you will see a warning similar to this:

warning: variable 'a' is uninitialized when used within its own
  initialization [-Wuninitialized]
A a(a);

this is the same as you call this function:

void assign(int& a){
    assign(a);
}
int main(){
    int a;
    assign(a);
    return 0;
}

no compilation error, this is a logical error

0
source

Source: https://habr.com/ru/post/1693037/


All Articles