Hell_Demon (09-18-2009),zeco (09-18-2009)
Ok since we're on a roll i'm posting an open ended question here:
Consider the following:
What are your thoughts on this function? why?Code:void MyClass::DoSomething() { //Some code here delete this; }
Hell_Demon (09-18-2009),zeco (09-18-2009)
slightly less confused:
I forgot 'this' means it is pointing to itself(the memory where the class is allocated after using new)
therefor this function would be a cleanup function amiright?
MyClass *testing = new MyClass; //allocate new memory for our class to put its shizzle in
//perform some random crap untill we no longer need it
//done performing random crap so lets clean up
testing->DoSomething(); //delete the allocated memory
edit:
ok wtf...
your post as it was before is in my quote, and now it says HI ARUN2 and HI ARUN1 o__O but it does not say 'last edited by' or anything this is scary :P
edit2:
HI ARUN1 dissappeared(it was at the end of the post) and the other one is still there
Last edited by Hell_Demon; 09-18-2009 at 02:54 PM.
huh? I did NOT put that in...
hmm you sorta follow what's going on but is that good or bad?
say you had a 3rd party DLL with that method in it that you have to call . Would you know it did that? Should it do that? why/why not?
Let me show something that would be a typical scenario..
What do you think about it now? Do you see any potential problems?Code:MyClass* pClass = new MyClass; ...Do Stuff on pClass pClass->DoSomething(); ...Do Stuff on pClass delete pClass;
Last edited by B1ackAnge1; 09-18-2009 at 03:12 PM.
Updated my post above yours with a sample that should more clearly show the issue
dosomething deletes the allocated memory therefor making the pointer point to NULL, after that you delete it again which will result into a problem im afraid.
edit: doing stuff after dosomething will also result in problems since its no longer there.
Correct - other valid answers would have been:
Find the guy who wrote that and beat'm
So this is something you NEVER want to do and is VERY BAD practice
Shoudl start keeping track of scores
It looks like a Deconstructor because it deletes the pointer to the object that it was called on. So in essence it deletes the object? I'm guessing you can't have two deconstructors in an object. What would happen?
Yeh I'm seeing the little Hi Arun thing too o_O its weird ...
Last edited by why06; 09-18-2009 at 03:39 PM.
"Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."- Dwight D. Eisenhower
Lets see:
<!--The function seems to do stuff, as indicated by the comment, and then delete the object that the function was called with.-->
By the way, i already read the question about why it's bad you posted to HD, and my answer is this is bad because the unsuspecting user might try to delete his pointer to the class that he dynamically allocated after he has already used that method. Which would be bad because you should never try to delete the same allocated memory twice.
Btw, BlackAngel, These supremely rock. Not only is this something for the C++ board community to do, but it is also telling us things that are expected of us to know, if we ever want a C++ programming job. Even if i get it completely wrong, that is good because i end up learning even more.
Last edited by zeco; 09-18-2009 at 04:21 PM.
B1ackAnge1 (09-18-2009),Hell_Demon (09-19-2009)