#include <iostream>
class A
{
public:
A(int n = 0) : m_n(n)
{
std::cout << 'd';
}
A(const A& a) : m_n(a.m_n)
{
std::cout << 'c';
}
private:
int m_n;
};
void f(const A& a1, const A& a2 = A())
{}
int main()
{
f(3);
}
谁能帮忙解释一下
void f(const A &a1, const A &a2 = A())
{}
尤其是 const A &a2 = A()
。
为什么 f(3)
?为什么整数输入仍然有效?应该是对象
最佳答案
因为在传递参数时,它被隐式转换为 A
。如果您在整数 ctor 参数列表后键入 explicit
,它将不再执行此操作。
关于c++ - 为什么函数中的对象输入不匹配但仍然有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27595881/