# Effective c++ Item19代码练习

先看如下代码：

#include <iostream>

using namespace std;

class rational {
public:
explicit rational(int numerator = 0, int denominator = 1);
int numerator() const;
int denominator() const;
const rational operator*(const rational& rhs) const;

private:
int n,d;
};
rational::rational(int numerator, int denominator):n(numerator),d(denominator)
{

}
int rational::numerator()const
{
return n;
}
int rational::denominator()const
{
return d;
}
const rational rational::operator*(const rational& rhs) const
{
return rational(this->numerator()*rhs.numerator(),this->denominator()*rhs.denominator());
}
int main()
{
rational oneeighth(1, 8);
rational result;
result = oneeighth * 2;
result = 2 * oneeighth;

return 0;
}

error C2679: binary '*' : no operator defined which takes a right-hand operand of type 'const int' (or there is no acceptable conversion)”

#include <iostream>

using namespace std;

class rational {
public:
rational(int numerator = 0, int denominator = 1);
int numerator() const;
int denominator() const;
const rational operator*(const rational& rhs) const;

private:
int n,d;
};
rational::rational(int numerator, int denominator):n(numerator),d(denominator)
{

}
int rational::numerator()const
{
return n;
}
int rational::denominator()const
{
return d;
}
const rational rational::operator*(const rational& rhs) const
{
return rational(this->numerator()*rhs.numerator(),this->denominator()*rhs.denominator());
}
int main()
{
rational oneeighth(1, 8);
rational result;
result = oneeighth * 2;
result = 2 * oneeighth;

return 0;
}

#include <iostream>

using namespace std;

class rational {
public:
rational(int numerator = 0, int denominator = 1);
int numerator() const;
int denominator() const;
private:
int n,d;
};
rational::rational(int numerator, int denominator):n(numerator),d(denominator)
{

}
int rational::numerator()const
{
return n;
}
int rational::denominator()const
{
return d;
}

const rational operator*(const rational& lhs,const rational& rhs)
{
return rational(lhs.numerator() * rhs.numerator(),
lhs.denominator() * rhs.denominator());
}
int main()
{
rational oneeighth(1, 8);
rational result;
result = oneeighth * 2;
result = 2 * oneeighth;

return 0;
}

#### Effective C++ 2e Item19

2001-07-10 21:21:00

#### 李开复给开发者七个建议

2017-01-09 22:48:35

#### Effective Modern C++:改善C++11和C++14的42个具体做法(中文版 + 英文版)

2017年11月22日 4.38MB 下载

#### 【C++】《Effective C++》读书笔记汇总

2016-04-06 11:14:19

#### 决定干点事儿--翻译一下《effective modern c++》

2016-01-09 00:27:50

#### Effective C++ 第三版（中文带书签PDF）

2009年05月31日 16.8MB 下载

#### Effective Modern C++笔记汇总

2017-06-05 16:09:58

#### 推荐1_Effective Modern C++(2017.11最新版)

2017年11月17日 4.24MB 下载

#### Effective Modern C++ 无水印pdf

2017年09月22日 3.87MB 下载

#### Effective C++(第3版)pdf

2017-07-18 21:07:14