Hi all!
I'm having some trouble with constructors and destructors. I've been told that a constructor creates an object and a destructor destroys it.
The problem is that in my code I try to destroy straight2
, but I can still print out it's length as if it wasn't destroyed at all. After that it gets destroyed again.
Can somebody please explain what I'm doing wrong?
#include <iostream>
using namespace std;
class Line
{
public:
int length;
Line(); // Constructor declaration
~Line(); // Destructor declaration
int getLength(void);
};
Line::Line()
{
cout << "Line constructed" << endl;
length = 10;
}
int Line::getLength(void)
{
return length;
}
Line::~Line()
{
cout << "Line destructed" << endl;
}
int main()
{
Line straight1;
Line straight2;
cout << "First line length: " << straight1.getLength() << endl;
straight2.~Line();
cout << "Second line length: " << straight2.getLength() << endl;
return 0;
}