类的已删除实例上的方法仍然有效? [英] method on deleted instance of class still work?

查看:72
本文介绍了类的已删除实例上的方法仍然有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Visual C ++ 2010上有这段代码

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");

}

当我运行它时:

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!

我有3个问题:

  1. 我第一次杀死实例"lucife"之后,为什么GetHealth()和GetMagic()这两种方法仍然有效?
  2. 我第二次在实例"lucife"上调用〜Human(),为什么它没有打印出"Lucife被杀!".像第一次?这里到底发生了什么?名称值被删除了吗?
  3. 返回Human :: health和返回this-> health是同一回事吗?我尝试了一下,发现没有区别.我认为他们两个都返回了在其上调用该方法的实例的运行状况(在这种情况下为"lucife").

非常感谢您

推荐答案

如@R和@Rupesh所述,您看到的是未定义的行为.

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.

  1. 我第一次杀死实例"lucife"后,为什么GetHealth()和GetMagic()两种方法仍然有效?

首先,不要像这样显式调用对象的destructor. 一旦退出范围main,它将自动被触发.这永远不是一个好习惯.

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 scope main. That's never a good practice.

您仍然可以调用GetHealth()GetMagic()的原因是,它们只是具有this的第一个隐藏参数的函数.

The reason you can still invoke GetHealth() and GetMagic() is that they are just functions with its first hidden argument to this.

如果看到

class AAA
{
public:
    void f() { std::cout << "Hello world" << std::endl; }
}

((AAA*) 0)->f();

可以编译并正常运行. (取决于您的环境).

able to be compiled and running okay. (depending on your environment).

因此,即使您在作用域中间明确地调用了一个析构函数,希望它能从字面上破坏内部的所有内容,您仍然可以在不引用nullptr的情况下到达this并成功地将this赋予预期的功能运气好.

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 dereferencing nullptr and successfully give this to the intended functions with any luck.

  1. 我第二次在实例"lucife"上调用〜Human(),为什么它没有打印出"Lucife被杀!".像第一次?到底是什么 发生在这里吗?名称值被删除了吗?
  1. 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?

这是因为当触发Human::~Human()时,也会触发std::string的析构函数,最终导致整理.

It's because when Human::~Human() is fired, the destructor of std::string gets also fired, which ends up tidying.

  1. 返回Human :: health和返回this-> health是同一回事吗?我尝试了一下,发现没有区别.我认为 它们返回调用该方法的实例的运行状况 (在这种情况下为"lucife".)
  1. 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).

不,它们是不同的. 但是,在您的代码中,Human::health只是被转换为this->health,因为您在类Human中使用了它.

No, they are different. However in your code, Human::health is just converted to this->health because you used that inside the class Human.

这篇关于类的已删除实例上的方法仍然有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆