此函数定义中附加的“:”是什么意思?
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.
}
我会搜索这个,但我不知道这种做法是什么。我刚刚开始学习模板,并在模板化的类中遇到过这个问题。
答案 0 :(得分:2)
这是你可以初始化成员变量的方法(你应该这样做)
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 ();
}
}
这是初始化列表 - 成员将使用给定值进行初始化。