衍生嘗試typename:
template <class T>
class CSafeSet
{
public:
CSafeSet();
~CSafeSet();
typename std::set<T>::iterator Begin();
private:
std::set<T> _Set;
};
您需要的類型名稱因爲它依賴於模板T.更多信息在代碼上方的鏈接中。這個東西很多的更加容易,如果你使用的typedef的:
template <class T>
class CSafeSet
{
public:
typedef T value_type;
typedef std::set<value_type> container_type;
typedef typename container_type::iterator iterator_type;
typedef typename container_type::const_iterator const_iterator_type;
CSafeSet();
~CSafeSet();
iterator_type Begin();
private:
container_type _Set;
};
在一個側面說明,如果你想完成你需要允許CSafeSet做同樣的事,作爲一個set could,這意味着使用自定義比較和分配器:
template <class T, class Compare = std::less<T>, class Allocator = std::allocator<T> >
class CSafeSet
{
public:
typedef T value_type;
typedef Compare compare_type;
typedef Allocator allocator_type;
typedef std::set<value_type, compare_type, allocator_type> container_type;
typedef typename container_type::iterator iterator_type;
typedef typename container_type::const_iterator const_iterator_type;
// ...
}
和建議的最後一位,如果你要創建一個圍繞類的包裝,儘量遵循相同的命名約定的類來自何處。也就是說,你的Begin()
應該可能是begin()
(並且我個人認爲C在類名奇怪之前,但是這取決於你:])
附註:請閱讀關於在標識符中使用下劃線的內容。 http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-ac-identifier/228797#228797 – 2009-10-13 18:39:49
短版本:「不要用下劃線開始標識符名稱「 – jalf 2009-10-13 19:03:46
長版本:後接大寫字母的下劃線保留給編譯器。雙下劃線後面的任何內容都保留給編譯器。 – GManNickG 2009-10-13 22:03:23