关于隐式类型转换
我定义了两个基类:Integer和Real,再派生出IntReal,现在想在IntReal重载加号,再定义类型转换构造函数,把基类转换成派生类,为什么重载的时候就不能识别呢?
程序如下:#include<iostream.h>
class Integer
{
public:
Integer(int a):i(a){}
Integer(const Integer&a){i=a.i;}
int i;
};
class Real
{
public:
Real(double a):f(float(a)){}
Real(const Real&a){f=a.f;}
float f;
};
class IntReal:public Integer,public Real
{
public:
IntReal(const Integer a):Integer(a),Real(0){}
IntReal(const Real a):Real(a),Integer(0){}
IntReal(int a=0):Integer(a),Real(0){}
IntReal(double a):Integer(0),Real(a){}
friend IntReal operator+(IntReal&a,IntReal&b);
};
IntReal operator+(IntReal&a,IntReal&b)
{
IntReal c(0);
if(a.f==0&&b.f==0)
c.i=a.i-b.i;
else
c.f=(float)a.i+a.f+(float)b.i+b.f;
return c;
}
void main()
{
Integer a(1),b(2),r1(0);
Real c(3),d(4),r2(0);
c+d;
}
编译时候报告不能进行类型转换:no operator defined which takes a left-hand operand of type 'class Real' (or there is no acceptable conversion)
请问这是为什么呢?我用vc6。