Destructors
What is the use of Destructors
Destructors are also special member functions used in C++ programming language. Destructors have the opposite function of a constructor. The main use of destructors is to release dynamic allocated memory. Destructors are used to free memory, release resources and to perform other clean up. Destructors are automatically named when an object is destroyed. Like constructors, destructors also take the same name as that of the class name.
General Syntax of Destructors
~ classname();
The above is the general syntax of a destructor. In the above, the symbol tilda ~ represents a destructor which precedes the name of the class.
Sample Class :
Class Definition
#ifndef GENES_H
#define GENES_H
#include <QtGui/QDialog>
namespace Ui {
class genes;
}
Q_OBJECT
Q_DISABLE_COPY(genes)
public:
explicit genes
(QWidget *parent
= 0);
virtual ~genes(); // automatically called when a dialog box is closed
protected:
virtual void changeEvent
(QEvent *e
);
private:
Ui::genes *m_ui;
};
#endif // GENES_H
#ifndef GENES_H
#define GENES_H
#include <QtGui/QDialog>
namespace Ui {
class genes;
}
class genes : public QDialog {
Q_OBJECT
Q_DISABLE_COPY(genes)
public:
explicit genes(QWidget *parent = 0);
virtual ~genes(); // automatically called when a dialog box is closed
protected:
virtual void changeEvent(QEvent *e);
private:
Ui::genes *m_ui;
};
#endif // GENES_H
To copy to clipboard, switch view to plain text mode
Class implementation
#include "genes.h"
#include "ui_genes.h"
// Construtor
{
m_ui->setupUi(this);
}
// Destructor
genes::~genes()
{
delete m_ui; // deallocate memory used by the class
#include "genes.h"
#include "ui_genes.h"
// Construtor
genes::genes(QWidget *parent) : QDialog(parent), m_ui(new Ui::genes)
{
m_ui->setupUi(this);
}
// Destructor
genes::~genes()
{
delete m_ui; // deallocate memory used by the class
To copy to clipboard, switch view to plain text mode
Bookmarks