Re: Regarding destruction
Sure, You are not accessing any *DATA* in the destructed object.
btw: This is still really a BAD practice.
Re: Regarding destruction
Quote:
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.
Quote:
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
Re: Regarding destruction
Ha! I like your analogy, Paul!
Viggy
Re: Regarding destruction
it is undefined behavior...
but should run on all compilers...
Re: Regarding destruction
Quote:
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.
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.....
Re: Regarding destruction
Quote:
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.
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
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.