繁体   English   中英

关于 C++ 中作用域解析运算符的混淆

[英]confusion about scope resolution operator in c++

:: 在线做什么: 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);
  }
};

像这样使用范围解析运算符::意味着调用全局operator newoperator delete函数,而不是可能已为该类覆盖的函数。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM