我想了解在使用一个形式而非另一个形式时有什么区别(如果有的话)。
代码 1(直接在变量上初始化):
#include <iostream>
using namespace std;
class Test
{
public:
Test() {
cout<< count;
}
~Test();
private:
int count=10;
};
int main()
{
Test* test = new Test();
}
代码 2(使用初始化列表在构造函数中初始化):
#include <iostream>
using namespace std;
class Test
{
public:
Test() : count(10) {
cout<< count;
}
~Test();
private:
int count;
};
int main()
{
Test* test = new Test();
}
语义上有任何区别,还是只是句法上的不同?