it is caused by memory destruction

jfhuang (Zigzag)
When print(X x) is invoked, a copy of object x is generated(say x1), x1's member px points at same memory address of member px of the orginal object x. Thus when print returns, x1 is released and destructor ~x is excuted to delete px.
When statement X y(16); is excuted, its constructor allocates a new memory for its member px at the same address as in object x and stores 16 in it. Since the member px of object x keeps same address as in y, print(x) will print 16 and destroy memory px. Afterwards, when print(y) is excuted, since memory px is deleted, a random value is printed and then an error about deleting unexist memory is raised.
It seems not easy to be understood. But if you can understand it completely, I think you can improve your c/c++ skills a lot.

2001-8-11 -04:00

回到话题: 国内一家公司招聘C++ Programmer的面试题:

回到论坛: HOME枫下论坛枫下论坛主坛工作学习IT技术讨论