It's heap management, a block of the size int was just deleted (first print(x)), then u try to alloc the same size (Y(16)), sure the heap manager will give u the same addr.

flipflop (翻云覆雨)
To avoid, if u must use pointer in class, u can:
1. everytime use this class type as parameter, pass it by pointer or reference (can make a private copy constructor to ensure it)
2. or implement ur own copy constructor, when copy, alloc another heap for the created new instance
3. or use some kind of smart pointer (keep monitoring the reference counter of the pointor, if it's 0 then do the real deletion)

Also, a good programming style: assign null to pointer after operator delete, and check if the pointor is null b4 u delete it
2001-8-11 -05:00

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

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