Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.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++_Memory Management_Destructor - Fatal编程技术网

C++ 正确的析构函数应该包含什么?

C++ 正确的析构函数应该包含什么?,c++,memory-management,destructor,C++,Memory Management,Destructor,我知道析构函数本质上是一个函数,它可以释放内存,或者在处理完后进行“清理” 我的问题是,在一个适当的析构函数中有什么 让我向您展示我拥有的一个类的一些代码: #ifndef TRUCK_H__ #define TRUCK_H__ #include <iostream> #include "printer.h" #include "nameserver.h" #include "bottlingplant.h" using namespace std; class Bottlin

我知道析构函数本质上是一个函数,它可以释放内存,或者在处理完后进行“清理”

我的问题是,在一个适当的析构函数中有什么

让我向您展示我拥有的一个类的一些代码:

#ifndef TRUCK_H__
#define TRUCK_H__

#include <iostream>
#include "printer.h"
#include "nameserver.h"
#include "bottlingplant.h"

using namespace std;

class BottlingPlant; // forward declaration

class Truck {

    public:
    Truck( Printer &prt, 
           NameServer &nameServer, 
           BottlingPlant &plant, 
           unsigned int numVendingMachines, 
           unsigned int maxStockPerFlavour );
    ~Truck();
    void action();

    private:
    Printer* printer;       // stores printer
    NameServer* ns;         // stores nameserver
    BottlingPlant* bottlingPlant;   // stores bottlingplant
    unsigned int numVM;     // stores number of vendingmachine
    unsigned int maxStock;      // stores maxStock
    unsigned int cargo[4];      // stores the cargo.

};
在我的析构函数类中,我应该在指针之后清理吗?就是, 将它们设置为空?还是删除它们

i、 e


感谢您的帮助,我只是想养成创建适当析构函数的好习惯。

在对象中存储指针时,您需要清楚地了解谁拥有它们指向的内存。如果您的类是所有者,则析构函数必须释放内存,否则将发生泄漏。如果您的类不是所有者,则不能释放内存

将点设置为NULL是不必要的,重要的是正确处理内存本身


管理指针的一种更简单的方法是使用智能指针类,该类将自动为您处理此问题。

由于您的指针不是从类中分配的,因此此处不会出现delete或NULL。因为指针是从类外部传递的,所以不要管它们


事实上,您正在传入引用,然后将它们转换为构造函数中的指针。这似乎没有必要。最好在内部使用它们作为参考。这取决于您的用例。如果您想使用指针,最好让构造函数接受指针。显式优于隐式。

这取决于构造函数中的内容。见:谢谢你的推荐。我一定要看一看。我已经添加了构造器供您查看。在阅读了Ned的答案后,我想您已经了解了,但我的意思是,您可能需要在使用前检查您的指针,因为有可能是
打印机的所有者删除了它,而
卡车
不知道,这会导致崩溃。@shengy打印机没有所有者。首先实例化打印机。:)@tf.rz您通过引用在中传递了
打印机
,因此必须有一个您定义的位置
打印机
,如果
打印机
被删除,在这种情况下,可能超出范围,并且您仍然在
主干
中使用它,这将导致崩溃。始终使用智能指针和其他形式的RAII,您将永远不必编写自己的析构函数-编译器自动生成的析构函数就足够了。啊,谢谢。我没有任何东西在里面,只是没有定义。我就这样吧@这是否意味着在他的情况下,他不能删除析构函数中的三个指针?因为他的三个指针都指向
Truck
没有的VAR。@Mark Random:有点夸张。在Alexandrescu的Loki中有一些通用的“范围卫士”,但有时为RAII管理编写自己的类更容易,因此您正在分解和本地化析构函数,而不是消除它们。其他时候,这可能是不必要的复杂化鞋钉的事情,如破坏登录到RAII机制。谢谢你的回答!我受到了一些限制,肯定会做一些不同的事情,但我会根据你和内德的回答让析构函数保持空的。
Truck::Truck( Printer &prt, 
              NameServer &nameServer, 
              BottlingPlant &plant, 
              unsigned int numVendingMachines, 
              unsigned int maxStockPerFlavour ) {
    printer = &prt;
    printer->print( Printer::Truck, 'S' ); 
    ns = &nameServer;
    bottlingPlant = &plant;
    numVM = numVendingMachines;
    maxStock = maxStockPerFlavour;
    cargo[ 0 ] = 0;
    cargo[ 1 ] = 0;
    cargo[ 2 ] = 0;
    cargo[ 3 ] = 0;
}//constructor
Truck::~Truck()
{
    printer = NULL; // or should this be delete printer?
    ns = NULL;
    bottlingPlant = NULL;
    // anything else? or is it fine to leave the pointers the way they are?
}//destructor