Segmentation Fault on object Creation
-
Hello All,
class Hello { public: Hello* obj = new Hello; inline Hello() { cout << "Constructor"; } inline ~Hello() { cout << "Destructor"; } };When i create an object of Hello , i am getting segmentation fault.
Can someone please help he understand the inner working on why segmentation fault happens.
Any help is deeply appreciated. Thanks in advance
-
Because it's an infinite recursion - with every creation of a Hello object you create a new one.
-
Hello All,
class Hello { public: Hello* obj = new Hello; inline Hello() { cout << "Constructor"; } inline ~Hello() { cout << "Destructor"; } };When i create an object of Hello , i am getting segmentation fault.
Can someone please help he understand the inner working on why segmentation fault happens.
Any help is deeply appreciated. Thanks in advance
@Vinoth-Rajendran4
Further to @Christian-Ehrlicher, in other words that means you need to delete the statementHello* obj = new Hello;! Your calling code will gonew Hello, and that will execute your constructor and return the created instance, you don't want anothernew Helloanywhere inside yourHelloclass as it stands.