Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
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)
 
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 25-07-2007, 09:46 PM   #2 (permalink)
I see right through you.
 
Sykora's Avatar
 
Join Date: Sep 2005
Location: Chennai
Posts: 597
Default 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 --
Sykora is offline  
Old 25-07-2007, 10:04 PM   #3 (permalink)
eWebGuru
 
ahref's Avatar
 
Join Date: Mar 2006
Location: Dehradun
Posts: 427
Default 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.
ahref is offline  
Old 25-07-2007, 11:39 PM   #4 (permalink)
Cool and Calm
 
abhi_10_20's Avatar
 
Join Date: Jul 2006
Location: Bangalore
Posts: 406
Default 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:
abhi_10_20 is offline  
Old 26-07-2007, 07:34 AM   #5 (permalink)
Human Spambot
 
Join Date: Nov 2004
Location: Madurai
Posts: 2,348
Default 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
sakumar79 is offline  
Old 26-07-2007, 06:42 PM   #6 (permalink)
fannedman
Guest
 
Posts: n/a
Post 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...
 
Old 26-07-2007, 09:58 PM   #7 (permalink)
God of Mistakes...
 
Garbage's Avatar
 
Join Date: Dec 2005
Location: Pune, Maharashtra
Posts: 1,923
Default 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.
__________________
Registered Linux User #468778
----------------------------------
http://twitter.com/_Garbage_
Garbage is offline  
Old 26-07-2007, 10:35 PM   #8 (permalink)
Cool and Calm
 
abhi_10_20's Avatar
 
Join Date: Jul 2006
Location: Bangalore
Posts: 406
Default 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:
abhi_10_20 is offline  
Old 27-07-2007, 03:25 PM   #9 (permalink)
God of Mistakes...
 
Garbage's Avatar
 
Join Date: Dec 2005
Location: Pune, Maharashtra
Posts: 1,923
Default 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 !!
__________________
Registered Linux User #468778
----------------------------------
http://twitter.com/_Garbage_

Last edited by Garbage; 27-07-2007 at 03:32 PM.
Garbage is offline  
Old 27-07-2007, 07:38 PM   #10 (permalink)
C# Be Sharp !
 
Zeeshan Quireshi's Avatar
 
Join Date: Jun 2006
Location: Toronto
Posts: 1,805
Default 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.
Zeeshan Quireshi is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
RoN query \/endett/\ Gamerz 10 26-06-2006 01:02 AM
RAM query ashfame Hardware Q&A 35 13-06-2006 10:44 AM
30 fps query Tommygecko Gamerz 1 22-05-2005 01:07 AM
Microtek Monitor Query and PHPBB Attachment Mod Query vwad Software Q&A 1 20-03-2005 09:32 AM

 
Latest Threads
- by Tenida
- by trublu
- by tv6952
- by soumya

Advertisement




All times are GMT +5.5. The time now is 04:18 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2