我想了解使用一种形式而不是另一种形式(如果有的话)有什么区别。
代码 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();
}
语义上有什么区别,还是只是句法?