|
-
February 13th, 2007, 12:35 PM
#1
Regarding destruction
Please have a look at below sample code:
Code:
#include<iostream.h>
class Test
{
public:
void fun()
{
cout<<"In fun"<<endl;
}
Test()
{
cout<<"In const"<<endl;
}
~Test()
{
cout<<"In dest"<<endl;
}
};
int main()
{
cout<<"Hello"<<endl;
Test * t = new Test();
t->fun();
delete t;
t->fun();
return 0;
}
In above I created object "t" and destroyed using delete operator.
After destroying also I am able to call the function "fun".How it is possible
output of above code :
Hello
In const
In fun
In dest
In fun
If i am able to call the function , than what actually destruction will do ?
-
February 13th, 2007, 12:38 PM
#2
Re: Regarding destruction
Sure, You are not accessing any *DATA* in the destructed object.
btw: This is still really a BAD practice.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
February 13th, 2007, 04:05 PM
#3
Re: Regarding destruction
 Originally Posted by mfdiqwer
In above I created object "t" and destroyed using delete operator. After destroying also I am able to call the function "fun".How it is possible
When an object's destructor is called, the object is no longer valid to use. This means you shouldn't be using it -- using it will exhibit undefined behaviour. This undefined behaviour could mean anything, including seemingly work correctly, or crash right away.
If i am able to call the function , than what actually destruction will do ?
Destruction doesn't mean to wipe it away from your program forever. All it means is that the object is no longer valid to use.
Just as if you are driving with an expired driver's license -- just because you have an expired license doesn't mean you can't physically get in a car and drive it around. Sure, you might be stopped by the police at some point, or you can drive for a 1,000 miles without any problems -- still, it's illegal to drive with an expired license.
Regards,
Paul McKenzie
-
February 13th, 2007, 04:40 PM
#4
Re: Regarding destruction
Ha! I like your analogy, Paul!
Viggy
-
February 13th, 2007, 04:43 PM
#5
Re: Regarding destruction
it is undefined behavior...
but should run on all compilers...
-
February 13th, 2007, 04:55 PM
#6
Re: Regarding destruction
 Originally Posted by Mitsukai
it is undefined behavior...
but should run on all compilers...
Correction: It should run MOST of the time on MOST common compilers.
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
February 13th, 2007, 05:04 PM
#7
Re: Regarding destruction
I know of no compilers where non-virtual methods that do not use any instance data actually become invalid.
That being said, it is still undefined.
As a side note, there are emerging "standards" which state that member functions which do not reference any instance data should be declared static, since they are not really specific to the instance.....
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
February 14th, 2007, 04:47 AM
#8
Re: Regarding destruction
 Originally Posted by TheCPUWizard
As a side note, there are emerging "standards" which state that member functions which do not reference any instance data should be declared static, since they are not really specific to the instance.....
Bad idea... There are cases where member functions happen, as an implementation detail subject to change, not to use any non-static member, but it doesn't mean that you've to declare this function as static because it's likely to evolve in future. Actually, there are even cases where we write virtual functions accessing no member!
But I guess that designing with a brain is harder than designing with stupid automatisms.
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
February 14th, 2007, 06:32 AM
#9
Re: Regarding destruction
You should set the pointer of object to 0 after deletion.
In addition, you should check if the pointer exists before calling methods
'You help me, and I, in turn, am helped by you!'
-H.S.
-
February 14th, 2007, 06:32 AM
#10
Re: Regarding destruction
HI Paul McKenzie
u had presentated nice example to explain destructor.
but what i read is that destructor frees the memory of that object so that we can resuse that memory for other purpose.
u tell that if driver's license expired then he can no longer use it legaly.
but what i think is that destructor means to destroy the users license so that he cannot use it anymore in future.
Paul McKenzie can u explain it clearly what exactly destructor do with an object.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|