P
PengYu.UT
Hi,
It seems that C++ does not allow overloading operators for primative
types, e.g. int, double. I'm wondering whether it is ture or there is
some walk-around?
Thanks,
Peng
#include <iostream>
#include <complex>
namespace my
{
int operator/(int x, int y) {
if(x>0)
return x/y;
else
return x/y - 1;
}
void test(int x, int y) {
std::cout << x << "/" << y << " " << x/y << std::endl;
}
}
int main() {
my::test(10, 3);
my::test(-10, 3);
my::test(10, -3);
my::test(-10, -3);
}
It seems that C++ does not allow overloading operators for primative
types, e.g. int, double. I'm wondering whether it is ture or there is
some walk-around?
Thanks,
Peng
#include <iostream>
#include <complex>
namespace my
{
int operator/(int x, int y) {
if(x>0)
return x/y;
else
return x/y - 1;
}
void test(int x, int y) {
std::cout << x << "/" << y << " " << x/y << std::endl;
}
}
int main() {
my::test(10, 3);
my::test(-10, 3);
my::test(10, -3);
my::test(-10, -3);
}