简体   繁体   中英

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:

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 eg 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 {
    ....
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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