关于c ++中范围解析运算符的混淆

时间:2017-01-16 11:22:05

标签: c++ templates

:: 排成一行: return :: operator new(size,:: std :: nothrow); 以及当模板类型T

没有用时,类为什么使用模板
template<typename T>
class DefaultMemoryAllocator
{
public:
  static inline void *Allocate(size_t size)
  {
    return ::operator new(size, ::std::nothrow);
  }
  static inline void Deallocate(void *pointer, size_t size)
  {
    ::operator delete(pointer);
  }
};

1 个答案:

答案 0 :(得分:1)

使用范围解析运算符 ::,这意味着调用全局 operator newoperator delete函数,而不是对那些可能已经被覆盖的人来说。

您可能会发现此函数是内存策略类的一部分,并且是从类operator newoperator delete重写函数调用的。