64,685
社区成员
发帖
与我相关
我的任务
分享
class Rational
{
public:
Rational(int a, int b)
: num(a), denom(b)
{}
void reduce() const
{
int gcd = 2;
Rational* let_me_modify = (Rational*)this;
let_me_modify->num /= gcd;
let_me_modify->denom /= gcd;
}
int num;
int denom;
};
class Rational2
{
public:
void reduce() const
{
int gcd = 2;
Rational* let_me_modify = (Rational*)this;
let_me_modify->num /= gcd;
let_me_modify->denom /= gcd;
}
int num;
int denom;
};
int _tmain(int argc, _TCHAR* argv[])
{
const Rational r(1,1);
r.reduce();
cout << r.denom << ' ' << r.num << endl;
Rational2 rTemp;
rTemp.denom = 1;
rTemp.num = 1;
const Rational2 r2(rTemp);
r2.reduce();
cout << r2.denom << ' ' << r2.num << endl;
return 0;
}