开发者

How to implement a custom class similar to a std::vector

开发者 https://www.devze.com 2023-03-05 10:32 出处:网络
My topic question is a little misleading, I dont want to implement a whole class like a std::ve开发者_Python百科ctor but I want to be able to create a class called Container so I can declare it like s

My topic question is a little misleading, I dont want to implement a whole class like a std::ve开发者_Python百科ctor but I want to be able to create a class called Container so I can declare it like so:

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 <>. The <> denotes that Container is a class template. 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 e.g. the C++ FAQ page about templates.


You should learn more about templates.
http://www.cplusplus.com/doc/tutorial/templates/

In a nutshell, what you want is:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号