#include <iostream>
using namespace std;
struct foo
{
};
namespace n1
{
void* operator new( size_t size )
{
void* raw = ::operator new( size );
cout << "alloc: " << raw << endl;
return raw;
}
void operator delete( void* raw )
{
cout << "free : " << raw << endl;
::operator delete( raw );
}
void test()
{
delete new int;
delete new foo;
}
}
int main()
{
n1::test();
return 0;
}
VS的Bug最大,它根本就不调用我重载的operator new和operator delete
g++的Bug很有趣,delete new foo 很正确,但 delete new int 却只调用了operator new,没调用 operator delete.
--------------- 最佳答案 ---------------
re: VS&G++ 重载operator delete的bug 2008-11-26 13:07 scu319hy
An allocation function shall be a class member function or a global function; a program is ill-formed if an allocation function is declared in a namespace scope other than global scope or declared static in global scope.
编译器没错,是你错了...
VC只是没有报错,而你的GCC才真的是错了.不过我用的4.3版结果很对.
责任编辑:小草