使用转换运算符对类对象进行引用
cout a class object using conversion operator
#include <iostream>
#include <cmath>
using namespace std;
class Complex
{
private:
double real;
double imag;
public:
// Default constructor
Complex(double r = 0.0, double i = 0.0) : real(r), imag(i)
{}
// magnitude : usual function style
double mag()
{
return getMag();
}
// magnitude : conversion operator
operator int ()
{
return getMag();
}
private:
// class helper to get magnitude
double getMag()
{
return sqrt(real * real + imag * imag);
}
};
int main()
{
// a Complex object
Complex com(3.0, 4.0);
// print magnitude
cout << com.mag() << endl;
// same can be done like this
cout << com << endl;
}
我不明白编译器如何解析调用转换运算符以进行cout << com << endl;
.
我还可以在同一类中有多个转换运算符。在这种情况下,将如何解决?
您已向 int
声明了一个转换运算符。由于此运算符不是显式的,因此编译器在查找 ostream::operator<<
的最佳重载时会考虑它。请记住,C++编译器始终尝试自动转换类型以查找匹配的调用,包括转换构造函数、转换运算符和隐式类型转换。
如果您不希望此行为,则可以从 C++11 开始将运算符标记为 explicit
:
explicit operator int() {
return getMag();
}
关于问题的第二部分,如果有多个同样好的转换,则会调用编译错误。如果你添加了,比如说,operator double
,那么当存在ostream::operator(double)
时,调用将是不明确的,你需要com
转换为你想要的类型。
我假设编译器尝试转换为 cout 定义了重载的类型。如果可以将其转换为 cout 定义了重载的 2 种类型,则会出现编译错误。如果将此函数添加到类代码中,则不会编译:
// magnitude : conversion operator
operator float ()
{
return getMag() + 1;
}
要解决这个问题,你必须做这样的强制转换:
// same can be done like this
cout << "Com: " << (float) com << endl;
相关文章:
- 返回常量对象引用 (getter) 和仅返回字符串有什么区别?
- 我们可以有一个 setter 成员函数作为从 const 对象引用的 const 吗?
- C++对象引用返回不同的值
- 对象引用中的字段以不同的方法返回不同的值
- 通过向构造函数其他对象引用页面来创建对象
- 为什么在对象引用恒定时允许更改为另一个类的指针的成员变量
- 如何在C++中读取 HDF5 对象引用
- C++ 未定义的对象引用
- 从函数返回 libconfig 类设置对象引用
- qt vsaddin错误对象引用未设置为对象的istanse
- 在这种情况下,为什么使用 *此必需的对象引用返回对象引用
- 基本C - 构造contaning对象引用,并将其作为方法参数传递
- C :将对象引用作为参数给出错误:呼叫class :: function()的匹配函数
- C++OOP基础知识-是否正确返回对象引用
- 使用对象引用(可能)在调用destructor后使用对象引用
- 创建返回对象引用的成员函数的最简单方法
- 如何在C++上使用对象引用删除对象
- JNI/Android NDK-维护全局对象引用
- 为什么有些类方法返回"*this"(self的对象引用)?
- 在C++中返回伪对象引用的规则