标题:c++中的析构函数
取消只看楼主
gold615
Rank: 2
等 级:论坛游民
帖 子:54
专家分:75
注 册:2014-6-7
结帖率:55.56%
已结贴  问题点数:20 回复次数:1 
c++中的析构函数
c++中的析构函数看书上说是程序运行到最后的时候会自动调用,但是如果在程序运行中调用呢?按理说一旦调用析构函数,变量就应该不存在了吧,但是这里却明确显示这个被提前析构后的参数依然存在,而且最后又被析构了一次,是程序自动跳过了主动析构函数的执行?我用的是cfree编译的。
#include<iostream>
using namespace std;
class Location
{
    public:
        Location(int x,int y);
        ~Location();
        int Get_X();
        int Get_Y();
        //void f(Location p);
    private:
        int X,Y;
};
Location::Location(int x,int y)
{
    cout<<"location called\n";
    X=x;Y=y;
}
Location::~Location()
{
    cout<<"destructor\n";
}
int Location::Get_X()
{
    return X;
}
int Location::Get_Y()
{
    return Y;
}
void f(Location p)
{
    cout<<p.Get_X()<<'\t'<<p.Get_Y()<<endl;
}
main()
{
    Location A(1,2);
    A.~Location();//为什么这个地方调用析构函数后,依下边的函数依然可以打印出A的元素?
    cout<<A.Get_X()<<endl;
    f(A);
}
搜索更多相关主题的帖子: private include public 而且 
2015-10-01 16:11
gold615
Rank: 2
等 级:论坛游民
帖 子:54
专家分:75
注 册:2014-6-7
得分:0 
确实好像看起来是这样 只能由系统自己调用
2015-10-01 22:28



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-457840-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.199773 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved