I've been messing around with c++ and attempting to fully understand certain behaviors. Given the following program:

#include <iostream>

class Test {
public:
	Test();
	~Test();
	
	void func();
};

Test::Test()
{
	std::cout << "Constructor" << std::endl;
}

Test::~Test()
{
	std::cout << "Destructor" << std::endl;
}

void Test::func()
{
	std::cout << "Deleting this..." << std::endl;
	delete this;
	std::cout << "Deleted this" << std::endl;
}

int main(int argc, char **argv)
{
	Test* ptr;

	ptr = new Test;
	ptr->func();
	delete ptr;
	
	std::cin.get();
	return 0;
}

I do realize one would almost never have a reason for "delete this." However, the odd thing I noticed is when the program reaches "delete ptr," it spits out an error (as it should), but when I view the console window it shows "Destructor" on the last line.

The output I am getting is this:

Constructor
Deleting this...
Destructor
Deleted this
Destructor

I am curious as to why "Destructor" outputs twice; I would think deleting an object that doesn't exist would cause the error prior to entering the destructor. I would assume (one should probably not assume when one is a newbie) the destructor can be called as it exists in the code, and it is only when the class itself finally dies (on the closing brace of the destructor) that it gives the error when it tries to delete the specific instance. I am still curious as to why the error is not chucked (or thrown, if you prefer) as soon as delete is called on an invalid object. Given a tad further thought as I am typing, I would assume this behavior is due to 'delete' calling the destructor prior to actually destroying the class, but I am not really basing any of this on anything but my brain.

Sooo, the real question is, am I correct in what I said? I haven't really run across any websites dealing with this, and I would hate to have the wrong ideas drilled into my head.

Thanks in advance.

vc++ 2008 Express produces a runtime error on that delete ptr; line because the class instance was already deleted. I've seen a few class objects delete themselves -- such as Microsoft's COM functions.

I mean, the destructor is executed twice. Once on delete this and again on delete ptr .

My question is, why is the destructor even called on delete ptr since ptr is pointing to an object that doesn't exist? If the object doesn't exist, how can the destructor be called a second time?

Is the ptr a NULL value?? No. Well just like any other invalid pointer the program doesn't know its invalid until delete attempts to delete it. You will get the same behavior if you try to delete any pointer, whether its a c++ class or not. You wrote the delete ptr; code, not the compiler. So if its wrong its your fault, not the compiler's. There is nothing about that pointer that would indicate the object has already been destroyed. When a c++ class is deleted only the data section of the class is destroyed, not the code section. As long as the code does not attempt to reference any of the class's data it should still work (undefined behavior though).

Didn't mean to fault the compiler; I realize my code is pretty much self-destructive. I now see how delete works with objects, though :).

Many thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.