c++ query

Status
Not open for further replies.
F

fannedman

Guest
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)
 

Sykora

I see right through you.
@First question :
As far as I can see, you've overridden the default destructor, so it doesn't really delete, only display your message. I really don't know how better to explain, other than to say that when you explicitly call the destructor, the object doesn't get deleted. I tried your code with some extras to test, and I found that I could access a member variable even after calling the destructor.

That said, no, you usually don't call the destructor explicitly.
 

abhi_10_20

Cool and Calm
but, by definition, a destructor's purpose is to delete an object.....

as far as i remember, wen an object of a type of particular class is created,
and if u havent invoked a destructor, the compiler will implicitly call the destructor to free up space occupied by the object....

surely no for 2nd one bcoz a base class pointer mainly lets us to access addresses of derived class objects only....
 

sakumar79

Technomancer
I think the way the objects work is that they are automatically removed when they go out of scope and that we cannot manually invoke the destructor to remove it... Not 100% sure though...

Try one thing... After the call to destructor, see if you can still access it (for eg, create a method hi to print hi, and call the method after the destructor call... It should work...

void main()
{
{
base test;
test.~base();
test.hi();
}
}

Arun
 
OP
F

fannedman

Guest
hm... from what i gather i think the destructor is called when the object is being destroyed ,it is not called to destroy the object..

Originally posted by abhi_10_20
surely no for 2nd one bcoz a base class pointer mainly lets us to access addresses of derived class objects only....
yes i'm aware of that. I'm sorry i didnt phrase the question properly.
What i actually want to do is create a simple base class and include a method say del() which destroys the object. Now this method del() when inherited should also be able to destroy the inherited object i.e del() method should be common to any inherited class without any modification, but now it seems the destructor does not delete the object...
 

Garbage

God of Mistakes...
Firstly let me clear you that the Definition of Distructor says that "This is a function which having same name that of class preceeded by a ~ sign & which automatically get executed when object of that class is being destroyed."

Now, as it is automatically executed when object is being destroyed, we can use it for Freeing memory. i.e. It is not it's duty to free memory. But we can use it's property to free the memory.

Secondly it never means that when destructor is called, object is deleted. But it means that when Object is deleted, destructor is called.
So, even after you called Destructor, object is still there.

Hope it is clear now. Isn't it ??
And let me know, if I'm wrong.
 

abhi_10_20

Cool and Calm
shirish_nagar said:
So, even after you called Destructor, object is still there.

r we calling the destructor?
i doubt...i think the destructor is calld automatically...see this...

class abc
{
data....
print()
~abc()
{
printf("Hello\n");
}
};

main()
{
abc a1;
a1.print();
}

the output wud contain "Hello" on last line......
this means when the object has been used and it is the end of the program, the destructor function is implicitly called to free up memory space allocated to object.....

wud this not mean that a destructor is called to destroy the object?
 

Garbage

God of Mistakes...
abhi_10_20 said:
r we calling the destructor?
i doubt...i think the destructor is calld automatically...see this...

class abc
{
data....
print()
~abc()
{
printf("Hello\n");
}
};

main()
{
abc a1;
a1.print();
}

the output wud contain "Hello" on last line......
this means when the object has been used and it is the end of the program, the destructor function is implicitly called to free up memory space allocated to object.....

wud this not mean that a destructor is called to destroy the object?
Now, look this....
U might be knowing what is the "scope of the object". So, when main() ends, every object must be destroyed!!! Thats why, when the object is being destroyed, destructor is automatically called !!!

And this doesn't mean that when destructor is called, object is destroyed. But reverse is TRUE !!
 
Last edited:

Zeeshan Quireshi

C# Be Sharp !
abhi_10_20 said:
but, by definition, a destructor's purpose is to delete an object.....

as far as i remember, wen an object of a type of particular class is created,
and if u havent invoked a destructor, the compiler will implicitly call the destructor to free up space occupied by the object....

surely no for 2nd one bcoz a base class pointer mainly lets us to access addresses of derived class objects only....
by definition , a destructor is a method which is called when an object is deleted .
 
Status
Not open for further replies.
Top Bottom