C++ primer 中文第三版 阅读笔记 第九、十章

时间:2022-02-24 04:19:15

一、函数重载

函数重载指名字相同,参数类型不同,和参数是否是const、参数名是否相同、返回类型无关

int f (int,int);

void f(int,int);

这个返回值类型不同,将认为void是int的重复定义,错误。

int f(int a)

int f(int b)

这个是同一个函数,不区分名字

int f(const int a)

int f(int a)

这声明的是同一个函数,不是重载

二、模板函数

1.

 template<class type>

type min(type a, type b);

2.

template<class type, int a>; 

type min(type p[a]);

在这个非class的a,它是int类型的,代表了type下的一个常量,可能是数组的长度等等,在函数中是可以使用size的,比如min(b[10]),那么a就是10

3.

模板参数名可以在多个函数模板声明或定义之间被重复使用,一个模板的定义和多个声明所使用的模板参数名无需相同

template<class T>

T min(T a){}


template<class T>

T max(T a, T b){}

这样是对的