/*
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;
}
c++学习-基础-重载函数:实参类型转换
最新推荐文章于 2024-07-20 10:17:32 发布