习题6-51
#include <iostream>
#include<vector>
using namespace std;
void f()
{
cout << "I LOVE YOU" << endl;
}
void f(int a)
{
cout << " I Love " << a<<endl;
}
void f(int a, int b)
{
cout << a << " Love" << b << endl;
}
void f(double a, double b)
{
cout << a << "hate" << b << endl;
}
void main()
{
//f(2.56, 42); //第一个会报错,有多个重载函数;
f(42);
f(42, 0);
f(2.56, 3.14);
}
习题6-52
(a)类型提升;
(b)算数类型转换;
习题6-53
(c)不合法,重复定义,P208页重载和const形参:“一个拥有顶层const的形参无法和另一个没有顶层const的形参区分开来”;
习题6-54
using F=int(*)(int,int)
vector<F> V1;
using F=int(int,int)
vector<F*> V1;
习题6-55-习题6-56
#include <iostream>
#include<vector>
using namespace std;
int add(int a, int b) { return a + b; }
int sub(int a, int b) { return (a - b); }
int multiply(int a, int b) { return a*b; }
int divide(int a, int b) { return b != 0 ? a / b : 0; }
void main()
{
typedef int(*p) (int, int);
vector<p> vec{ add, sub,multiply, divide };
for (auto f : vec) {
cout << f(2, 4) << endl;
}
}
#include <iostream>
#include<vector>
using namespace std;
int add(int a, int b) { return a + b; }
int sub(int a, int b) { return (a - b); }
int multiply(int a, int b) { return a*b; }
int divide(int a, int b) { return b != 0 ? a / b : 0; }
void main()
{
using p= int(*)(int, int);
vector<p> vec = { &add,&sub,multiply,divide }; //直接用函数名和用取址函数名等价
for (auto f : vec)
cout << f(2, 4) << endl;
}