In the following piece of code, what is the meaning of ::~
GaussianMex::~GaussianMex()
{
int i;
}
In the following piece of code, what is the meaning of ::~
GaussianMex::~GaussianMex()
{
int i;
}
This is not a single operator ::~
, but a definition of GaussianMex destructor. You define class methods by ClassName::ClassMethod
syntax and since destructor name is ~ClassName
resulting destructor definition is ClassName::~ClassName
.
This is a destructor.
Consider:
class GaussianMex
{
public:
// This is the CONstructor (ctor). It is called when an instance of the class is created
GaussianMex()
{
};
// This is a Copy constructor (cctor). It is used when copying an object.
GaussianMex(const GaussianMex& rhs)
{
};
// This is a move constructor. It used when moving an object!
GaussianMex(GaussianMex&& rhs)
{
};
// This is the DEStructor. It is called when an instance is destroyed.
~GaussianMex()
{
};
// This is an assignment operator. It is called when "this" instance is assigned
// to another instance.
GaussianMex& operator = (const GaussianMex& rhs)
{
return *this;
};
// This is used to swap two instances of GaussianMex with one another.
friend void swap(GaussianMex& lhs, GaussianMex& rhs)
{
};
}; // eo class GuassianMex
The purpose of a constructor is to do any initialisation that is required (perhaps allocating memory, or other class instances). The destructor does the opposite - it performs the clean up of any resources the class allocated during its' lifetime.
It indicates that the method is a destructor.
Your class can have at most one destructor, and it is always the name of the class prefixed with the ~
symbol.
The destructor is called whenever an instance of your object is destroyed. This happens whenever an instance goes out of scope, or when you call delete
on a pointer to an instance the the class.