Thread: c++ query
View Single Post
Old 25-07-2007, 09:26 PM   #1 (permalink)
fannedman
Guest
 
Posts: n/a
Question c++ query

I didnt know where to ask such questions,i hope i have posted in the right section.

Ok, c++ is an object oriented programming language and is mostly centered around objects.

Suppose base is a simple class with a simple constructor and destructor, like :

class base {
int x;
public:
base(){cout<<"base constructor called\n";}
~base(){cout<<"base destructor called\n";}
}

and the output of a simple program like:

void main()
{
{
base test;
}
}
will be:
base constructor called
base destructor called

But when you call the destructor manually( i dnt know if this done in any standard practice) the destructor is called twice!
void main()
{
{
base test;
test.~base();
}
}
base constructor called
base destructor called
base destructor called

So my first question is : am i doing anything wrong here, since the object was already destroyed, why is its destructor being called again? or more precisely , can or should an object's destructor be called manually?

In this example, there are no errors even if the destructor is called twice, but if a destructor frees allocated memory or similar tasks, you'll end up with lots of errors after execution

My second question:
Suppose der is inherited from the base class, is there any way i can destroy the derived object through a base class pointeror can i invoke its destructor (provided my first question has a solution)