class A
{
public:
int a;
int b;
int c;
};
int main()
{
A f ( ); //不行
A c {1,2};
A d {1,2,3};
}
當對象的數據成員是public,在創建對象時可以在初始化列表中指定他們的值。
class A
{
public:
int a;
int b;
int c;
A(int d,int e,int f) {}
};
int main()
{
A c{1,2}; //no instance of constructor "A::A" matches the argument list;
A c{1,2,3};
A c(1,2); //no instance of constructor "A::A" matches the argument list;
A c(1,2,3);
}
注意調用構造函數完全不同於包含公共數據成員值的初始化列表中提供的語句,而此處初始化列表包含構造函數的實參,有三個形參,所以列表中必須有三個值。