编程语言
首页 > 编程语言> > c++ 转换函数 convert function

c++ 转换函数 convert function

作者:互联网

学习侯捷老师视频,做了如下测试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);	
}

标签:function,convert,int,double,fra,c++,operator,侯捷,include
来源: https://blog.csdn.net/shihuangdeng898/article/details/120256803