Click to See Complete Forum and Search --> : Regarding destruction
mfdiqwer
February 13th, 2007, 11:35 AM
Please have a look at below sample 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 ?
TheCPUWizard
February 13th, 2007, 11:38 AM
Sure, You are not accessing any *DATA* in the destructed object.
btw: This is still really a BAD practice.
Paul McKenzie
February 13th, 2007, 03:05 PM
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 possibleWhen 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
MrViggy
February 13th, 2007, 03:40 PM
Ha! I like your analogy, Paul!
Viggy
Mitsukai
February 13th, 2007, 03:43 PM
it is undefined behavior...
but should run on all compilers...
SuperKoko
February 13th, 2007, 03:55 PM
it is undefined behavior...
but should run on all compilers...
Correction: It should run MOST of the time on MOST common compilers.
TheCPUWizard
February 13th, 2007, 04:04 PM
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.....
SuperKoko
February 14th, 2007, 03:47 AM
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.
eero_p
February 14th, 2007, 05:32 AM
You should set the pointer of object to 0 after deletion.
delete t;
t = 0;
In addition, you should check if the pointer exists before calling methods
if (t)
t->fun();
nitin1979
February 14th, 2007, 05:32 AM
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.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.