代码如下
class Student
{
string name;
public:
Student(string n = "1234")
{
cout << "Construing student " + n + "\n";
name = n;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Student haha ("haha");
return 0;
}
为什么这里输出的是haha,而不是1234
解决方案
5
调用的时候写了参数自然就不会在使用默认参数了啊。
10
你传了参数进行构造当然不会使用默认值”1234″了
Student haha;这样写才行
Student haha;这样写才行
5
搜索 c++ 默认参数
5
“1234”是默认参数,在你不提供参数(Student haha )时,才使用默认参数,否则使用你提供的参数