Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 析构函数和释放函数之间有什么区别?_C++_Oop_Destructor_Object Lifetime - Fatal编程技术网

C++ 析构函数和释放函数之间有什么区别?

C++ 析构函数和释放函数之间有什么区别?,c++,oop,destructor,object-lifetime,C++,Oop,Destructor,Object Lifetime,在这段代码中,析构函数和释放函数有什么区别 我不明白,它们在我看来是一样的。它们做着完全相同的事情,你为什么需要像DeAllocate这样的函数呢?析构函数有自动调用的好处,但取消分配没有 #include <iostream> using namespace std; const int SIZE=5; class ARRAY_CLASS { public: ARRAY_CLASS();//default constructor ~ARRAY_CLASS();

在这段代码中,析构函数和释放函数有什么区别

我不明白,它们在我看来是一样的。它们做着完全相同的事情,你为什么需要像DeAllocate这样的函数呢?析构函数有自动调用的好处,但取消分配没有

#include <iostream>
using namespace std;

const int SIZE=5;

class ARRAY_CLASS
{
public:
    ARRAY_CLASS();//default constructor
    ~ARRAY_CLASS(); //destructor
    void Add(int); //mutator
    void Print(); //accessor
    int * Get_Address(); //accessor
    void DeAllocate(); //mutator
private:

    int *A;
    int count;
};

ARRAY_CLASS::ARRAY_CLASS()
{
    cout<<"Default constructor has been called\n";
    A = new int[SIZE];
    count = 0;
}


ARRAY_CLASS::~ARRAY_CLASS()
{
    cout<<"The Destructor has been Called!\n";
    delete [ ] A;
    A=0;
    count = 0;
}

void ARRAY_CLASS::Add(int item)
{
    if (count<SIZE)
        A[count++]=item;
    else
        cout<<"Array Full\n";
}
void ARRAY_CLASS::Print()
{
    for(int i=0; i<count; i++)
        cout<<"A[i] = "<<A[i]<<endl;
}

int * ARRAY_CLASS::Get_Address()
{
    return A;
}

void ARRAY_CLASS::DeAllocate()
{
    delete [ ] A;
    A = 0;
    count = 0;
}

int main()
{

    ARRAY_CLASS B;

    B.Add(1);
    B.Add(2);
    B.Add(3);
    B.Add(4);
    B.Add(5);

    B.Print();

    ARRAY_CLASS A = B;

    cout<<"A holds address location = "<<A.Get_Address()
    <<" and B holds address location "<<B.Get_Address()<<endl;

    B.DeAllocate();
    A.Print();

    return 0;
}
#包括
使用名称空间std;
常数int SIZE=5;
类数组\类
{
公众:
数组_CLASS();//默认构造函数
~ARRAY_CLASS();//析构函数
void Add(int);//mutator
void Print();//访问器
int*Get_Address();//访问器
void DeAllocate();//mutator
私人:
int*A;
整数计数;
};
数组_类::数组_类()
{

cout当对象本身被移除时,析构函数将发生


在您发布的示例中,您可以简单地将该函数重命名为其他函数,比如说
void ARRAY\u CLASS::ClearArray()
,而不是
void ARRAY\u CLASS::DeAllocate()
。您所做的只是释放A使用的内存,而不是销毁整个对象。

确实可以重写析构函数:

ARRAY_CLASS::~ARRAY_CLASS()
{
    cout<<"The Destructor has been Called!\n";
    DeAllocate();
}
ARRAY\u CLASS::~ARRAY\u CLASS()
{

你可以不调用析构函数。当你删除一个对象或它失去作用域时会发生这种情况。在你发布的示例中,如果数组是你唯一担心清除的数据,你可以让析构函数调用deallocate函数。析构函数有自动调用的好处,但deallocate没有。就是这样。Seriouly.看不远了。RAII依赖于此。在大多数语言中,甚至在托管语言中也存在相同的概念(例如,考虑C语言中的
IDisposable
结合使用
)。这让我们的日常生活变得更好。离题:强烈推荐
A=nullptr;
而不是
A=0;
。第一种方法的意图对于普通读者来说更为明显。
DeAllocate()
方法可能根本不存在。它会使对象处于无效状态。应该只有析构函数。