游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2185|回复: 3

关于析构函数~

[复制链接]

5

主题

42

帖子

42

积分

注册会员

Rank: 2

积分
42
发表于 2010-10-29 16:29:00 | 显示全部楼层 |阅读模式
本来以为类的析构函数就是释放掉某个对象,释放完了该内存应该也释放了,就像通过指针delete一样,但是做了个小测试发现有点问题:


  1. #include <iostream>
  2. using namespace std;

  3. class  base
  4. {
  5. public:
  6.         base(int a)
  7.         {cout<<a<<" "<<"base con"<<endl;}
  8.         virtual ~base()
  9.         {cout<<a<<" "<<this<<" "<<"base ~con"<<endl;}
  10. private:
  11.         int a;
  12.         base &operator=(const base &a);
  13. };

  14. class derived:public base
  15. {
  16. public:
  17.         derived(int a):base(a)
  18.         {cout<<"d1 con"<<endl;}
  19.         ~derived()
  20.         {cout<<"d1 ~con"<<endl;}
  21. private:
  22.         derived &operator=(const derived &d);
  23. };

  24. void main()
  25. {
  26.         //base *b=new derived(2);
  27.         //delete b;
  28.         derived d(2);
  29.         d.~derived();
  30. }
复制代码

假如采用new和delete方式,结果和我想象的一样,输出构造和析构函数各一次,然后如果采用创建对象然后调用其析构函数的方式则会输出两遍析构函数,求解

5

主题

263

帖子

1113

积分

金牌会员

Rank: 6Rank: 6

积分
1113
发表于 2010-10-29 16:57:00 | 显示全部楼层

Re:关于析构函数~

动态开辟的空间可以调用delete函数自动析构,但是静态开辟的(也就是在类的后面定义对象的),不能手动使用delete析构,只能属于哪个括号的析构就在哪里。另外《数据结构(严蔚敏版)》中,大量使用了指针作为类的元素。因为指针的作用可以使用malloc()函数或new语句来动态开辟空间,有了这些就可以动态释放的,如果你在一个大括号中无法析构这个对象的话,之多它包含的是几个指针的值,而指针所指向的空间已经被释放了。另外,可以看看我的代码。这是我自己定义的一个类:JStack。
#ifndef _J_DEFINE_H_
#define _J_DEFINE_H_
//Using C++ style
template<typename CustomType>//可将任意类型应用于此
struct CustomStruct
{
        CustomType elem;
        CustomStruct* link;
};
template<typename CustomType>
class JStack
{
public:
        JStack():base(NULL),top(NULL),JStackSize(NULL){}//默认构造函数
        ~JStack(){}//默认析构函数
        bool IsEmpty( void );//判断是否为空栈
        CustomType GetTop( void );//取出栈顶的元素
        CustomType Pop( void );//将元素从栈中弹出
        bool Push(CustomType e);//将元素压入栈顶
private:
        CustomStruct<CustomType>* base;//栈底
        CustomStruct<CustomType>* top;//栈顶
        int JStackSize;//栈的元素个数
};//栈结构的定义
#endif






#include "JStackDefine.h"

template<typename CustomType>
bool JStack<CustomType>::IsEmpty( void )//判断是否为空栈
{
        if ( top == base )
                return true;
        return false;
}

template<typename CustomType>
bool JStack<CustomType>:ush(CustomType e)//将元素压入栈顶
{
        CustomStruct<CustomType>* temp;
        temp = top;
        top = new CustomStruct<CustomType>;
        if ( !top )
                return false;
        top->elem = e;
        top->link = temp;
        JStackSize++;
        return true;
}//此方法相当于头插法

template<typename CustomType>
CustomType JStack<CustomType>::Pop( void )//将元素从栈中弹出
{
        CustomStruct<CustomType> temp;
        temp.elem = top->elem;
        temp.link = top->link;
        delete top;
        top = temp.link;
        JStackSize--;
        return temp.elem;
}

template<typename CustomType>
CustomType JStack<CustomType>::GetTop( void )//取出栈顶的元素
{
        return top->elem;
}

1

主题

49

帖子

49

积分

注册会员

Rank: 2

积分
49
发表于 2010-10-29 18:55:00 | 显示全部楼层

Re:关于析构函数~

delete是delete,释放是释放,对象不同...

5

主题

42

帖子

42

积分

注册会员

Rank: 2

积分
42
 楼主| 发表于 2010-10-29 22:09:00 | 显示全部楼层

Re:关于析构函数~

终于搞清楚了  
事实上这个代码漏写了一句话,导致自己被搞糊涂了
那也就是说一个对象被创建后不遇到右括号是不会消失的, 手动调用其析构函数是无效的.
看来只有用指针来创建释放了  
彩阳多谢...
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-6-1 19:20

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表