2017-01-16 77 views
2

什么::确实符合:return :: operator new(size,:: std :: nothrow); 以及为什么类使用模板时,有没有使用模板类型T关于范围解析运算符在C++中的困惑

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

它是“范围解析运算符”。此类型的原因在这里解释http://stackoverflow.com/questions/4173254/what-is-the-curiously-recurring-template-pattern-crtp – StoryTeller

回答

1

使用范围解析操作::像这意味着全球operator newoperator delete函数被调用,如反对那些可能已经被这个班重写的人。

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

+0

为什么类没有使用模板时使用模板T型? – tohidprogram

+2

@tohidprogram如果您分享了课程的使用地点和内容,可能会更容易说出来。 – Angew

+0

它看起来像它是基于奇怪的循环模板模式(CRTP - Google)的策略类型框架的一个片段。 *特定的成员函数不使用'T',但其他人可能会这样做。 – Bathsheba