学习侯捷老师视频,做了如下测试code
#include<iostream>
#include<vector>
#include<string>
//#include<Windows.h>
using namespace std;
class fra
{
public:
fra(int i,int j ) :m_i(i),m_j(j) {
}
operator double() const
{
return (double) m_i/ (double) ( m_j);
}
private:
int m_i;
int m_j;
} ;
int main()
{
fra f(3,2);
cout << f.operator double() <<endl; // 1.5
double a = 1+ f +0.1;
cout << a<< endl; //2.6
return 0;
}
其中转换函数与侯捷老师写的不同,按老师写的不能实现转换功能,仅供参考
operator double() const
{
return (double) m_i/ (double) ( m_j);
}