在类中可以定义引用类型的成员变量,但是必须在构造函数之前完成初始化,也就是必须在构造函数的初始化列表中完成初始化。
class Functor
{
public:
// The constructor.
explicit Functor(int& evenCount)
: _evenCount(evenCount)
{
//_evenCount = evenCount;
}
// The function-call operator prints whether the number is
// even or odd. If the number is even, this method updates
// the counter.
void operator()(int n)
{
cout << n;
if (n % 2 == 0)
{
cout << " is even " << endl;
// Increment the counter.
_evenCount++;
}
else
{
cout << " is odd " << endl;
}
}
private:
int& _evenCount; // the number of even variables in the vector
};注意如果将初始化列表去掉,改为在构造函数中初始化,则编译器会提示:
error C2758: 'Functor::_evenCount' : must be initialized in constructor base/member initializer list
本文深入探讨了在C++中如何在构造函数的初始化列表中完成引用类型成员变量的初始化,强调了在类定义中进行正确初始化的重要性,并通过实例展示了具体实现方式。

被折叠的 条评论
为什么被折叠?



