We’ve seen that a special member function—the constructor—is called automatically when an object is first created. You might guess that another function is called automatically when an object is destroyed. This is indeed the case. Such a function is called a destructor. A destructor has the same name as the constructor (which is the same as the class name) but is preceded by a tilde:
class Foo
{
private:
int data;
public:
Foo() : data(0) //constructor (same name as class)
{ }
~Foo() //destructor (same name with tilde)
{ }
};
Like constructors, destructor s do not have a return value. They also take no arguments (the assumption being that there’s only one way to destroy an object).
Saturday, November 1, 2008
Destructors
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment