c++学习-基础-重载函数:实参类型转换

/*
	Date: 05/03/21 16:36
	Description: 重载函数:实参类型转换
	转换等级
		1.精确匹配
		2.通过类型提升
		3.通过标准转换
		4.通过类类型转换
	参数匹配和枚举类型
	重载和const形参 
*/
#include<iostream>

using namespace std;

class Account
{
public:
	Account(){}
	Account(int x):num(x){}
public:
	int num;
};

enum Tokens
{
	INLINE = 128,
	VIRTUAL = 129
};

void lookup(Account &x)
{
	cout<<"lookup(Account &x)"<<endl;
}

void lookup(const Account &x)
{
	cout<<"lookup(const Account &x)"<<endl;
}

void f(int *p)
{
	cout<<"f(int *p)"<<endl;
}

//void f(int *const p)  //指针是const,指向的对象不是const 	[Error] redefinition of 'void f(int*)'
//{
//	cout<<"f(int *const p)"<<endl;
//}

void f(const int *p)  //指针不是const,指向的对象是一个const 
{
	cout<<"f(const int *p)"<<endl;
}

void newf(unsigned char x)//	[Error] two or more data types in declaration of 'newf'
{
	cout<<"newf(unsigned char x)"<<endl;
}

void newf(int y)
{
	cout<<"newf(int y)"<<endl;
}

void ff(Tokens t)
{
	cout<<"ff(Tokens t)"<<endl;
}

void ff(int x)
{
	cout<<"ff(int x)"<<endl;
}

void ff(short y)
{
	cout<<"ff(short y)"<<endl;
}


void manip(double x)
{
	cout<<"manip(double x)"<<endl;
}

void manip(long x)
{
	cout<<"manip(long x)"<<endl;
}

void maip(float y)
{
	cout<<"manip(float y)"<<endl;
}




int main()
{
	int m = 5,n = 6;
	int *p = &m;
	const int *p2 = &n;
	
	f(p);
	f(p2);
	
	const Account a(0);
	Account b;
	lookup(a);
	lookup(b);
	
	Tokens curTok  =  INLINE;
	ff(curTok);
	
	unsigned char uc = 129;
	newf(VIRTUAL);//newf(int y)
	newf(uc);
	
	ff(128);
	ff('a'); //类型提升  char类型优先转换为int  
	manip(3.14);//manip(double x)  优先于  manip(long x)  优先于  manip(float x)
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值