class中cout顺序的问题

C++语言 码拜 8年前 (2016-04-27) 904次浏览
代码如下

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;这样写才行

5

搜索 c++ 默认参数

5

“1234”是默认参数,在你不提供参数(Student haha )时,才使用默认参数,否则使用你提供的参数

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明class中cout顺序的问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)