重载类型转换运算符 [隐式显式转换]

在这里插入图片描述

#include<iostream>
#include<vector>
#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<set>
#include<queue>
#include<unordered_map>
#include<cmath>
using namespace std;
class Complex
{
	double real, imag;
public:
	Complex(double r = 0, double i = 0) :real(r), imag(i) {};
	operator double() {
		return real;
	}
};
int main()
{
	Complex c(1.2, 3.4);
	cout << (double)c << endl;
	double n = 2 + c;
	cout << n;
}
原文地址:https://www.cnblogs.com/Hsiung123/p/13811941.html