Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default Constructor Recursion Crash

Tags:

c++

/*The following program seems to mysteriously enter recursion even though there is none in sight. Compiler: g++ (Ubuntu 4.4.3-4ubuntu5.1) 4.4.3 Machine: x86 OS: Ubuntu 10.04 64-bit

*/

    #include<iostream>
    using namespace std;

    class Test
    {
    public:
      Test ():x(9)
      {
        cout << " Test::Test\n";
        Test (x);
      }
      Test (int a)
      {
        cout << " Test::para\n";
      }
    private:
            int x;
    };


int main(void)
{
 Test a;
return 0;
}

Why is this?

like image 975
PeerPandit Avatar asked Jul 03 '26 12:07

PeerPandit


1 Answers

Test (x);

is parsed as

Test x;

... not as a constructor call. You can also write

Test (y);

and get the same behaviour.

like image 146
wolfgang Avatar answered Jul 06 '26 01:07

wolfgang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!