【发布时间】:2020-03-20 04:16:46
【问题描述】:
这段代码有友元函数和运算符重载,我得到的输出部分有意义,所以这是代码,我没有得到的是构造函数如何具有当在 中进行的调用带有对象参数时,将调用浮点类型参数。
class a
{
public:
a():n(3),d(2)
{
cout<<endl<<"default constructor called";
}
a(float f):n(f),d(1)
{
cout<<endl<<"type casting constructor called";
}
a(const a &f)
{
cout<<endl<<"copy constructor called";
}
friend a operator+( a x, a y);
};
a operator+(a x, a y)
{
return x;
}
主要部分到此结束
int main()
{
a f1;
float f=1.5;
f1+f;
}
问题究竟是如何调用参数化构造函数或类型转换构造函数?
Output:
default constructor called
type casting constructor called
copy constructor called
copy constructor called
...
【问题讨论】:
-
将浮点构造函数标记为显式,然后看看会发生什么
-
代码无法编译,因为没有
n或d的声明。
标签: c++ operator-overloading copy-constructor default-constructor friend-function