I know a destructor is essentially a function that deallocates memory or does a "clean up" whenever you are done with it.
My question is, what goes in a proper destructor?
Let me show you some code for a class I have:
#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.
};
Here is the constructor:
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
In my destructor class, should I be cleaning up after the pointers? That is, setting them to NULL? or deleting them?
i.e
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
Thank you for any help, just want to get in to a good habit of creating proper destructors.