WEB开发网
开发学院软件开发VC VS&G++ 重载operator delete的bug(已解决) 阅读

VS&G++ 重载operator delete的bug(已解决)

 2009-04-17 20:03:36 来源:WEB开发网   
核心提示:#include <iostream>using namespace std;struct foo{};namespace n1{ void* operator new( size_t size ) { void* raw = ::operator new( size ); cout << &q

#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版结果很对.

Tags:VS amp 重载

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接