下面那种情形下myfunc函数声明是重载?
A. int myfunc(int a,double b) 和 double myfunc(int a, double b)
B. int myfunc(int a,double b) 和 int myfunc(int a,double b=0.5)
C. int myfunc(int a,double b) 和 int myfunc(double b,int a)
D. int myfunc(int a,double b) 和 double myfunc(int , double )
其余都是覆盖。
- 下面那种情形下myfunc函数声明是重载?
A. namespace IBM{
int myfunc(int a);
}
namespace SUN{
int myfunc(double b);
}
B. namespace IBM{
int myfunc(int a);
}
namespace SUN{
using IBM::myfunc;
int myfunc(double b);
}
C. namespace IBM{
int myfunc(int a);
namespace SUN{
int myfunc(double b);
}
}
D. class A{
public:
int myfunc(int a);
}
class SubA: public A{
public:
int myfunc(double b);
}
考察作用域和重载的关系。重载是基于一个作用域的。
B在SUN作用域里进行了重载。
重载关键是要在一个作用域里。
D的情况实际是覆盖。
标准里规定,如果子类具有与父类相同的函数名(即使参数不同),就会覆盖父类中的函数
class A{
public:
int myfunc(int a){ return 0;}
};
class SubA: public A{
public:
int myfunc(int* p){return 0;}
};
SubA sa;
sa.myfunc(1); //报错,说不能将int转换为 int*;
- 下面说法正确的是:
A. 操作符重载可以改变操作符的运算优先级。
B. 操作符重载时,其预定义的操作数可以改变。例如“!”是一元操作符,可以重载成有两个参数的二元操作符。
C. C++中所有的操作符都可以被重载。
D. 对于内置数据类型的操作符,它预定义的意义不能被修改;也不能为内置数据类型定义其它操作符。例如不能定义int operator+(int,int);也不能定义void operator+(int[],int[])。
优先级,结合性,操作数的个数都不能改变
不能被重载的运算符:?: . :: sizeof .* # https://blog.youkuaiyun.com/weixin_39187443/article/details/113393598
//int myfunc(double,double) {cout << __LINE__<
- 重载函数的调用匹配规则
#define _myfunc1(x) myfunc1(x) {cout << __FUNCTION__ << " " << typeid(x).name() << " ";}
#define _myfunc2(x) myfunc2(x) {cout << __FUNCTION__ << " " << typeid(x).name() << endl;}
void _myfunc1(bool); //void _myfunc2(bool);
void _myfunc1(char); //void _myfunc2(char);
void _myfunc1(unsigned char); //void _myfunc2(unsigned char);
void _myfunc1(unsigned short); //void _myfunc2(unsigned short);
void _myfunc1(int); void _myfunc2(int);
void _myfunc1(unsigned int); void _myfunc2(unsigned int);
void _myfunc1(long); void _myfunc2(long);
void _myfunc1(unsigned long); void _myfunc2(unsigned long);
void _myfunc1(float); void _myfunc2(float);
void _myfunc1(double); void _myfunc2(double);
int main()
{
myfunc1((bool)0); myfunc2((bool)0);//myfunc1 bool //myfunc2 int
myfunc1((char)0); myfunc2((char)0);//myfunc1 char //myfunc2 int
myfunc1((unsigned char)0); myfunc2((unsigned char)0);//myfunc1 unsigned char //myfunc2 int
myfunc1((short)0); myfunc2((short)0);//myfunc1 short //myfunc2 int
myfunc1((unsigned short)0); myfunc2((unsigned short)0);//myfunc1 unisgned short //myfunc2 int
myfunc1((int)0); myfunc2((int)0);//myfunc1 int //call to myfunc2 is ambiguous
myfunc1((unsigned int)0); myfunc2((unsigned int)0);//myfunc1 int //call to myfunc2 is ambiguous
return 0;
}
#define _myfunc(x, y) myfunc(x, y) {cout << __FUNCTION__ << " "<< typeid(x).name() << " " << typeid(y).name() <<endl; return 0;}
int _myfunc(double,double);
int _myfunc(short,double);
double _myfunc(long,float);
double _myfunc(int,double);
int main()
{
int a = myfunc((unsigned short)65530,20.0); // myfunc int double
return 0;
}
重载函数的调用匹配,依次按照下列规则来判断:
精确匹配:参数匹配而不做转换,或者只是做微不足道的转换,如数组名到指针、函数名到指向函数的指针、T到const T;
提升匹配:即整数提升(如bool到int、char到int、short到int);
使用标准转换匹配:如int到double、double到int、double到long double、Derived*到Base*、T*到void*、int到unsigned int;
使用用户自定义匹配;
使用省略号匹配:类似于printf中省略号参数。
假如运行环境int类型4bytes,short类型2bytes,long类型8bytes,存在代码:
int a = myfunc( (unsigned short)65530, 20.0 );
会优先匹配以下哪一个重载函数?
A. int myfunc( double, double )
B. int myfunc( short, double )
C. double myfunc( int, float )
D. double myfunc( int, double)