简体   繁体   English

如何实现类似于std :: vector的自定义类

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

My topic question is a little misleading, I dont want to implement a whole class like a std::vector but I want to be able to create a class called Container so I can declare it like so: 我的主题问题有点误导,我不想实现像std :: vector这样的整个类,但是我希望能够创建一个名为Container的类,以便可以这样声明:

Container <unsigned int> c;

So is this how I overload the <> operator... 这就是我如何重载<>运算符...

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

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

There is no operator <> . 没有operator <> The <> denotes that Container is a class template . <>表示Container是一个类模板 You need syntax along the lines of: 您需要遵循以下语法:

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

The best place to start is to find a good C++ book, but you could also try reading eg the C++ FAQ page about templates . 最好的起点是找到一本好的C ++书籍,但是您也可以尝试阅读有关模板C ++ FAQ页面

You should learn more about templates. 您应该了解有关模板的更多信息。
http://www.cplusplus.com/doc/tutorial/templates/ http://www.cplusplus.com/doc/tutorial/templates/

In a nutshell, what you want is: 简而言之,您想要的是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM