繁体   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