struct X
{
int i;
X(int in)
{
cout << "converting int " << in << " to X\n";
i = in;
}
operator+(int in)
{
cout << "adding X " << i << " + int " << in << "\n";
int ret = i + in;
cout << "op+(X,int) returns " << ret << "\n";
return ret;
}
};
struct Y
{
int i;
Y(X Xin)
{
cout << "converting X " << Xin.i << " to Y\n";
i = Xin.i;
}
operator+(X Xin)
{
cout << "adding Y " << i << " to X " <<
Xin.i << "\n";
int ret = i + Xin.i;
cout << "op+(Y,X) returns " << ret << "\n";
return ret;
}
operatorint()
{
cout << "converting Y " << i <<
" to int\n";
return i;
}
};
X operator* (X Xin, Y Yin)
{
cout << "multiplying X " << Xin.i << " times Y " <<
Yin.i << "\n";
X Xout(Xin.i * Yin.i);
cout << "op*(X, Y) returns " << Xout.i << "\n";
return Xout;
}
int f(X Xin)
{
cout << "calling f(X = " << Xin.i << ")\n";
return Xin.i;
}
X x = 1;
Y y = x;
int i = 2;
main()
{
int ret = i + 10;
cout << "i + 10 = " << ret << "\n";
ret = y + 10;
cout << "y + 10 = " << ret << "\n";
ret = y + 10 * y;
cout << "y + 10 * y = " << ret << "\n";
ret = x + y + i;
cout << "x + y + i = " << ret << "\n";
ret = x * x + i;
cout << "x * x + i = " << ret << "\n";
ret = f(7);
cout << "f(7) = " << ret << "\n";
// illegal conversion
// ret = f(y);
// cout << "f(y) = " << ret << "\n";
ret = y + y;
cout << "y + y = " << ret << "\n";
ret = 106 + y;
cout << "106 + y = " << ret << "\n";
return 0;
}
struct X
{
int i;
X(int in)
{
cout << "converting int " << in << " to X\n";
i = in;
}
intoperator+(int in)
{
cout << "adding X " << i << " + int " << in << "\n";
int ret = i + in;
cout << "op+(X,int) returns " << ret << "\n";
return ret;
}
};
struct Y
{
int i;
Y(X Xin)
{
cout << "converting X " << Xin.i << " to Y\n";
i = Xin.i;
}
int Y::operator+(X Xin)
{
cout << "adding Y " << i << " to X " <<
Xin.i << "\n";
int ret = i + Xin.i;
cout << "op+(Y,X) returns " << ret << "\n";
return ret;
}
operatorint()
{
cout << "converting Y " << i <<
" to int\n";
return i;
}
};
X operator* (X Xin, Y Yin)
{
cout << "multiplying X " << Xin.i << " times Y " <<
Yin.i << "\n";
X Xout(Xin.i * Yin.i);
cout << "op*(X, Y) returns " << Xout.i << "\n";
return Xout;
}
int f(X Xin)
{
cout << "calling f(X = " << Xin.i << ")\n";
return Xin.i;
}
X x = 1;
Y y = x;
int i = 2;
int main()
{
int ret = i + 10;
cout << "i + 10 = " << ret << "\n";
ret = y + 10;
cout << "y + 10 = " << ret << "\n";
ret = y + 10 * y;
cout << "y + 10 * y = " << ret << "\n";
ret = x + y + i;
cout << "x + y + i = " << ret << "\n";
ret = x * x + i;
cout << "x * x + i = " << ret << "\n";
ret = f(7);
cout << "f(7) = " << ret << "\n";
// illegal conversion
// ret = f(y);
// cout << "f(y) = " << ret << "\n";
ret = y + y;
cout << "y + y = " << ret << "\n";
ret = 106 + y;
cout << "106 + y = " << ret << "\n";
return 0;
}
You have an ambiguous overload, meaning that when you call ret = y + 10 the compiler has 2 options and no way of knowing what you mean.
Do you mean use y as being a int and store the sum in ret, or do you mean do y + 10 and then store y in ret as being a int.
In other words is y a int or object of type Y in this context?
#include <iostream>
usingnamespace std;
struct X
{
int i;
X(int in)
{
cout << "converting int " << in << " to X\n";
i = in;
}
intoperator+(int in)
{
cout << "adding X " << i << " + int " << in << "\n";
int ret = i + in;
cout << "op+(X,int) returns " << ret << "\n";
return ret;
}
};
struct Y
{
int i;
Y(X Xin)
{
cout << "converting X " << Xin.i << " to Y\n";
i = Xin.i;
}
intoperator+(X Xin)
{
cout << "adding Y " << i << " to X " <<
Xin.i << "\n";
int ret = i + Xin.i;
cout << "op+(Y,X) returns " << ret << "\n";
return ret;
}
operatorint()
{
cout << "converting Y " << i <<
" to int\n";
return i;
}
};
X operator* (X Xin, Y Yin)
{
cout << "multiplying X " << Xin.i << " times Y " <<
Yin.i << "\n";
X Xout(Xin.i * Yin.i);
cout << "op*(X, Y) returns " << Xout.i << "\n";
return Xout;
}
int f(X Xin)
{
cout << "calling f(X = " << Xin.i << ")\n";
return Xin.i;
}
X x = 1;
Y y = x;
int i = 2;
int main()
{
int ret = i + 10;
cout << "i + 10 = " << ret << "\n";
ret = (int)y + 10;
cout << "y + 10 = " << ret << "\n";
ret = (int)y + 10 * (int)y;
cout << "y + 10 * y = " << ret << "\n";
ret = x + y + i;
cout << "x + y + i = " << ret << "\n";
ret = x * x + i;
cout << "x * x + i = " << ret << "\n";
ret = f(7);
cout << "f(7) = " << ret << "\n";
// illegal conversion
// ret = f(y);
// cout << "f(y) = " << ret << "\n";
ret = y + y;
cout << "y + y = " << ret << "\n";
ret = 106 + y;
cout << "106 + y = " << ret << "\n";
return 0;
}