简体   繁体   中英

What does the additional colon mean in a templated class. ClassName<T, SIZE>::ClassName:

What does the additional ':' mean in this function definition?

template <class T, int SIZE> 
class Buffer
{
 public: 
    Buffer();
 private: 
    int _num_items; 
};

template <class T, int SIZE> 
Buffer<T, SIZE>::Buffer() :
    _num_items(0) //What does this line mean?? 
{
   //Some additional code for constructor goes here. 
}

I would search this, but i have no idea what this practice is called. I have just started learning about templates, and encountered this in a templated class.

This is how you can initialize member variables (and you should do that)

class Something
{
private:
  int aValue;
  AnotherThing *aPointer;

public:
  Something() 
   : aValue(5), aPointer(0)
  {
     printf(aValue); // prints 5
     if(aPointer == 0) // will be 0 here
       aPointer = new AnotherThing ();
  }
}

It's the initialisation list - the members will be initialized with the given value.

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