C++ 在模板类中,额外的冒号是什么意思。类名<;T、 尺寸>;::类名:

C++ 在模板类中,额外的冒号是什么意思。类名<;T、 尺寸>;::类名:,c++,class,templates,constructor,colon,C++,Class,Templates,Constructor,Colon,在这个函数定义中,附加的“:”是什么意思 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??

在这个函数定义中,附加的“:”是什么意思

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. 
}
模板
类缓冲区
{
公众:
缓冲区();
私人:
整数项;
};
模板
Buffer::Buffer():
_num_items(0)//这行是什么意思??
{
//这里有一些构造函数的附加代码。
}

我会搜索这个,但我不知道这种做法叫什么。我刚刚开始学习模板,并在模板类中遇到了这一点

这就是初始化成员变量的方法(您应该这样做)


这是初始化列表-成员将用给定的值初始化。

搜索“构造函数初始化列表”谢谢您的回答,所以这只适用于模板类还是适用于任何类?任何类都可以这样初始化其成员。在您的例子中,它是模板类,但这个初始化列表适用于C++中的所有类。只需谷歌
c++构造函数初始化列表
谢谢。这个网站太棒了。有些东西我甚至不知道从哪里开始找。不客气。真的是石头!如果你想接受答案,你可以点击左边的复选标记,这样用户就可以看到这个问题已经得到了答案。顺便说一句:)我一直在等待,因为我想你必须等待10分钟才能选择答案。这使我走上了正确的道路。
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 ();
  }
}