 |
25-07-2007, 09:26 PM
|
#1 (permalink)
|
|
Guest
|
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)
|
|
|
|
|
Advertisements. Register and be a member of the community to get rid of them.
|
|
Advertisement
|
|
25-07-2007, 09:46 PM
|
#2 (permalink)
|
|
I see right through you.
Join Date: Sep 2005
Location: Chennai
Posts: 597
|
Re: c++ query
@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.
__________________
I didn't make the world, I only try to live in it.
http://lucentbeing.com
-- Sykora --
|
|
|
25-07-2007, 10:04 PM
|
#3 (permalink)
|
|
eWebGuru
Join Date: Mar 2006
Location: Dehradun
Posts: 427
|
Re: c++ query
Yes, when you call destructor, object is not destroyed only method is executed.
__________________
Windows and linux hosting at http://www.ewebguru.com
Get $50 per blog post PM me for details.
|
|
|
25-07-2007, 11:39 PM
|
#4 (permalink)
|
|
Cool and Calm
Join Date: Jul 2006
Location: Bangalore
Posts: 406
|
Re: c++ query
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....
__________________
When Roger's at play, opponents pray..!!!!! :twisted:
|
|
|
26-07-2007, 07:34 AM
|
#5 (permalink)
|
|
Human Spambot
Join Date: Nov 2004
Location: Madurai
Posts: 2,348
|
Re: c++ query
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
|
|
|
26-07-2007, 06:42 PM
|
#6 (permalink)
|
|
Guest
|
Re: c++ query
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..
Quote:
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...
|
|
|
|
26-07-2007, 09:58 PM
|
#7 (permalink)
|
|
God of Mistakes...
Join Date: Dec 2005
Location: Pune, Maharashtra
Posts: 1,923
|
Re: c++ query
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.
|
|
|
26-07-2007, 10:35 PM
|
#8 (permalink)
|
|
Cool and Calm
Join Date: Jul 2006
Location: Bangalore
Posts: 406
|
Re: c++ query
Quote:
|
Originally Posted by shirish_nagar
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?
__________________
When Roger's at play, opponents pray..!!!!! :twisted:
|
|
|
27-07-2007, 03:25 PM
|
#9 (permalink)
|
|
God of Mistakes...
Join Date: Dec 2005
Location: Pune, Maharashtra
Posts: 1,923
|
Re: c++ query
Quote:
|
Originally Posted by abhi_10_20
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 by Garbage; 27-07-2007 at 03:32 PM.
|
|
|
27-07-2007, 07:38 PM
|
#10 (permalink)
|
|
C# Be Sharp !
Join Date: Jun 2006
Location: Toronto
Posts: 1,805
|
Re: c++ query
Quote:
|
Originally Posted by abhi_10_20
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 .
__________________
There are 10 types of people in the world: those who understand binary and those who do not.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|