3

The program is something 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) fixes the problem.

Is it something in the A a(a) line that is dangerous?

William
  • 942
  • 2
  • 12
  • 25
  • 2
    Related: https://stackoverflow.com/questions/32608458/is-passing-a-c-object-into-its-own-constructor-legal – scohe001 Feb 01 '18 at 19:15
  • 5
    Sounds like a bug of your debugger, hopelessly trying to unravel this mysterious chain of `A`s, oblivious to the fact that it is chasing its own tail. – Quentin Feb 01 '18 at 19:16
  • How can you construct an instance, using the same instance, when the instance hasn't been finished constructing? – Thomas Matthews Feb 01 '18 at 19:51
  • I'm unable to duplicate the lock-up, but while copy constructing oneself is syntactically legal, logically it's not a very good idea. Need a [mcve] to really get to the bottom of this. – user4581301 Feb 01 '18 at 20:55

1 Answers1

0

It's infinite recursion, Apparently A classes constructor is calling itself again and again . if you use gcc you will see warning like this :

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

it is just like you are calling this function:

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

there is no compilation error, it's logical error

Ali Safaya
  • 56
  • 9