The statement delete pt; deletes the object of the class it points to, however it's still a pointer to the a class obj (whether it exists or not). Class member functions still exist, but the object and its data members are deleted.
Compile this and see what it does...
Code:
#include <iostream>
using namespace std;
class TestClass
{
public:
TestClass();
~TestClass();
void test(){ cout << "test " << i << endl; }
private:
int i;
};
TestClass::TestClass()
: i(24)
{
cout << "Constructor called" << endl;
}
TestClass::~TestClass()
{
cout << "Destructor called" << endl;
}
int main()
{
cout << "initializing TestClass *objPtr = new TestClass; instantiates objPtr" << endl;
TestClass *objPtr;
objPtr = new TestClass;
cout << "\nobjPtr->test(); calls member function from TestClass before delete" << endl;
objPtr->test();
cout << "\ndeleting objPtr destroys object" << endl;
delete objPtr;
objPtr = NULL;
cout << "\ncout << objPtr << endl; verifies pointer set to NULL" << endl;
cout<< objPtr <<endl;
cout << "\nobjPtr->test(); calls member function from deleted TestClass obj" << endl;
objPtr->test();
return 0;
}