assume tst is name of the class
after delete ptr how i can destroyed rObj ?
thanks
Code:tst *ptr = new tst() ; tst &rObj = *ptr; delete ptr ;
(I beg your pardon for my english)
assume tst is name of the class
after delete ptr how i can destroyed rObj ?
thanks
Code:tst *ptr = new tst() ; tst &rObj = *ptr; delete ptr ;
(I beg your pardon for my english)
Well... you do have a problem there don't you?
rObj will be only destroyed when it goes out of scope. What you should do though is set ptr to null. That will ensure rObj will not yield undefined behavior.
EDIT: I'm not sure but I can bet you are strongly discouraged to create references to dereferenced dynamically allocated objects. And that is exactly the reason why.Code:int *ptr = new int(12) ; int &rObj = *ptr; delete ptr; std::cout << rObj // undefined behaviour ptr = 0; std::cout << rObj // ok.
Last edited by Mario F.; 08-13-2006 at 10:04 AM.
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
>> after delete ptr how i can destroyed rObj ?
You can't "destroy" rObj, you just have to make sure you don't use it anymore.
>> That will ensure rObj will not yield undefined behavior.
No, actually it won't. All you can do is make sure you don't use rObj after you delete ptr. What do you expect the output of the second cout to be?
Yes. I was not sure about it to be honest. Should have included a small note saying so.
I was expecting the second output to generate a runtime error. Which would always be preferable than undefined behavior. But after your post I tested it, and it doesn't.
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
yes,I can't change rObj value after initialization
thanks