簡體   English   中英

如何實現類似於std :: vector的自定義類

[英]How to implement a custom class similar to a std::vector

我的主題問題有點誤導,我不想實現像std :: vector這樣的整個類,但是我希望能夠創建一個名為Container的類,以便可以這樣聲明:

Container <unsigned int> c;

這就是我如何重載<>運算符...

class Container
{
   private:
      Container() 
      {
         ...
      }

   public:
      void operator <>( unsigned int )
      {
         // what do I put here in the code?
         // maybe I call the private constructor...
         Container();
      }
};

沒有operator <> <>表示Container是一個類模板 您需要遵循以下語法:

template <typename T>
class Container
{
    ...
};

最好的起點是找到一本好的C ++書籍,但是您也可以嘗試閱讀有關模板C ++ FAQ頁面

您應該了解有關模板的更多信息。
http://www.cplusplus.com/doc/tutorial/templates/

簡而言之,您想要的是:

template <class T>
class Container {
    ....
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM