I am new to C++. I have written below code for understanding how constructor and destructor works in C++.
#include<iostream>
using namespace std;
class Line
{
private:
Line();
public:
int length;
static void getInstance(Line* objLine);
~Line();
};
void Line::getInstance(Line* objLine)
{
if(objLine == NULL)
{
objLine = new Line();
}
}
Line::Line()
{
cout<<"In the Constructor"<<endl;
}
Line::~Line()
{
cout<<"In the Destructor"<<endl;
}
int main()
{
Line* objLine = NULL;
Line::getInstance(objLine);
return 0;
}
I have read that destructor of a class is invoked when object goes out of scope. In above code object is handled by objLine which is a local variable. So at the end of the main i have expected destructor to be invoked. But it is never getting invoked. Any one please tell me when destructor is invoked in above case