我知道这是一个糟糕的形式,默认值应该在声明中指定,但如果你愿意放纵我一会儿..为什么这个编译?到底发生了什么?
#include <iostream>
using namespace std;
class test
{
public:
test(int n);
};
test::test(int n = 666)
{
cout << n;
}
int main()
{
test t;
cin.sync();
cin.ignore();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出: 666
..模板如何影响同一段代码?
template <class T>
class test
{
public:
test(int n);
};
template <class T>
test<T>::test(int n = 666)
{
cout << n;
}
int main()
{
test<int> t;
cin.sync();
cin.ignore();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误:没有适当的默认构造函数可用
感谢您的时间!