内联函数 函数重载 函数缺省参数
内联函数:
函数调用是有时间开销的。如果函数本身只有几条语 句,执行非常快,而且函数被反复执行很多次,相比 之下调用函数所产生的这个开销就会显得比较大。 为了减少函数调用的开销,引入了内联函数机制。编 译器处理对内联函数的调用语句时,是将整个函数的 代码插入到调用语句处,而不会产生调用函数的语句。
inline int Max(int a,int b) { if( a > b) return a; return b; }
函数重载:
一个或多个函数,名字相同,然而参数个数或参数类 型不相同,这叫做函数的重载。 以下三个函数是重载关系: int Max(double f1,double f2) { } int Max(int n1,int n2) { } int Max(int n1,int n2,int n3) { } 函数重载使得函数命名变得简单。 编译器根据调用语句的中的实参的个数和类型判断应 该调用哪个函数。(1) int Max(double f1,double f2) { } (2) int Max(int n1,int n2) { } (3) int Max(int n1,int n2,int n3) { } Max(3.4,2.5); //调用 (1) Max(2,4); //调用 (2) Max(1,2,3); //调用 (3) Max(3,2.4); //error,二义性。
特别声明:以上内容(如有图片或视频亦包括在内)为自媒体平台“网易号”用户上传并发布,本平台仅提供信息存储服务。
Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.