簡體   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