#define   DEBUG_NEW   new(THIS_FILE,   __LINE__)   
  void*   operator   new(size_t   nSize,   LPCSTR   lpszFileName,   int   nLine)   
  {   
  return   NULL;   
  }   
  #define   new   DEBUG_NEW   请问
new(THIS_FILE,   __LINE__)   
  void*   operator   new(size_t   nSize,   LPCSTR   lpszFileName,   int   nLine) 
怎么参数个数不一样呢?

解决方案 »

  1.   

    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif
      

  2.   

    CObject 对 operator new 有 3个重载形式CObject::operator new void* PASCAL operator new( 
       size_t nSize  
    );
    void* PASCAL operator new( 
       size_t, 
       void* p  
    );
    void* PASCAL operator new( 
       size_t nSize, 
       LPCSTR lpszFileName, 
       int nLine  
    );In the Debug version, operator new participates in an allocation-monitoring scheme designed to detect memory leaks.If you use the code line
    #define new DEBUG_NEWbefore any of your implementations in a .CPP file, then the second version of new will be used, storing the filename and line number in the allocated block for later reporting. You do not have to worry about supplying the extra parameters; a macro takes care of that for you. Even if you do not use DEBUG_NEW in Debug mode, you still get leak detection, but without the source-file line-number reporting described above.
      

  3.   

    #ifdef _DEBUG 
    #define new DEBUG_NEW 
    #undef THIS_FILE 
    static char THIS_FILE[] = __FILE__; 
    #endif
      

  4.   

    new CMyClass实际上自动传入了一个sizeof(CMyClass)参数,明白了吧。
      

  5.   

    int* p = new(THIS_FILE, __LINE__) int;
    函数的第1参数是sizeof(int),第2参数是THIS_FILE,第3参数是__LINE__。
      

  6.   

    operator new 必须要求第一次个参数是size_t, 后面的是没有硬性规定, 可以随便定义。
    比如
    operator new(size_t, int, int, int, int)
    则通过 new(int, int, int, int) class 的代码调用这个函数只是c++规定了2个默认的实现,
    一个是
    operator new(size_t)   // 普通调用 new class
    一个是
    operator new(size_t, void*) // replacement new, new (address) class 
    直接把address这个地址返回vc的DEBUG_NEW之需要记录这个LPCSTR  lpszFileName,  int  nLine两个信息,
    所以实现了你看到的这个函数
    经过了上述的宏后
    new class 就变成了
    new(THIS_FILE,  __LINE__) class
    于是编译器计算了下class的size后,开始调用下列函数,并期待返回一个可用的内存快。
    void* operator  new(size_t  nSize,  LPCSTR  lpszFileName,  int  nLine);