I have this code on Visual C++ 2010
#include <iostream>
#include <string>
using namespace std;
class Human {
private:
int magic;
int health;
string name;
public:
int GetMagic() const;
int GetHealth() const;
Human(int, string);
~Human();
};
//helper
int Human::GetHealth() const {
cout <<"This returns Human::health" << endl;
return Human::health;
}
int Human::GetMagic() const {
cout <<"This returns this->magic"<< endl;
return this->magic;
}
//con/destructor
Human::Human(int a, int b, string c): health(a), magic(b), name(c)
{
cout<<c<<" is born!"<<endl;
}
Human::~Human()
{
cout <<this->name << " is killed!" << endl;
}
int main (){
Human lucife(20,10,"Lucife");
cout << lucife.GetHealth()<<endl;
cout << lucife.GetMagic()<<endl;
lucife.~Human();
cout << lucife.GetHealth()<<endl;
cout << lucife.GetMagic()<<endl;
cout<<endl;
lucife.~Human();
system("pause");
}
And when I run it:
Lucife is born!
This returns Human::health;
20
This returns this->magic
10
Lucife is killed!
This returns Human::health
20
This returns this->magic
10
is killed!
I have 3 questions:
- After I killed instance "lucife" the first time, why did the 2 methods GetHealth() and GetMagic() still worked?
- The second time I called ~Human() on instance "lucife", why didn't it print out "Lucife is killed!" like the first time? What exactly happened here? Is the name value deleted?
- Does return Human::health and return this->health mean the same thing? I tried it and see that there is no difference. I think both of them return the health of the instance on which the method was called ("lucife" in this case).
Many thanks to you
I'm still learning about stackoverflow and those who post here. Strange environment....
Okay... First, use a pointer to your pointer to your object, then your "const" declarations and the variables defined... I'm not quite sure what your goal is here but they're mismatched (in the way you're using them). Next, you don't need to use the "this" pointer in your method.
Remember that memory management (in C++) is YOUR responsibility. If you need / want to call the destructor, then do it through the "delete" process.
Human *pLucifer = new Human (20,10,"Lucifer");
cout << pLucifer->GetHealth()<GetMagic()<
delete pLucifer;
Disregard the other answer posts you've received as the "Human" object was created on the stack and even though you've called the destructor explicitly, the memory management will not be processed until the function terminates (in this case... main). So the reason your data is still accessible after you've explicitly called the destructor (naughty! Don't do that) is because the stack is still in tact and however you reference it (the stack), you'll be able to get what data resides there. Hope it helps
You are seeing the symptoms of undefined behavior.
From the C++ Standard:
Might be the reason why the contents are still there (correct me if I'm wrong).
From C++, Free-Store vs Heap,
And I agree with @R Sahu that what you are trying to do is undefined.
As @R and @Rupesh mentioned, you are seeing the behavior that's undefined.
However, if I may provide you with a little more explanation about what's going on in your very specific code in that specific environment, this might be it.
First of all, do not call your object's
destructor
explicitly like that. It will automatically be fired as soon as you get out of the scopemain
. That's never a good practice.The reason you can still invoke
GetHealth()
andGetMagic()
is that they are just functions with its first hidden argument tothis
.You might be surprised if you see
able to be compiled and running okay. (depending on your environment).
So even if you invoked a destructor explicitly in the middle of scope hoping that it will literally destruct everything inside, you might still be able to reach
this
without dereferencingnullptr
and successfully givethis
to the intended functions with any luck.It's because when
Human::~Human()
is fired, the destructor ofstd::string
gets also fired, which ends up tidying.No, they are different. However in your code,
Human::health
is just converted tothis->health
because you used that inside the classHuman
.