2017-01-16 123 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