形参指的是在函数的形参列表中声明的局部变量。用实参初始化形参形参列表中的形参通常用逗号隔开,其中每个形参都是含有一个声明符的声明。

实参指的是函数调用时提供的值,用于初始化函数的形参。

 

(1)f()函数返回类型和f函数返回的值的类型不同,把f()函数改成string f()

(2)未定义f2()函数的返回类型,把f2()函数改成void f2(int i)

(3)形参名相同,把calc()函数改成int calc(int v1,int v2)

(4)函数体没有加花括号限制范围

 

  1. template <typename T>
  2. T fact(T val) {
  3. T ret = val;
  4. while (val > 1) {
  5. ret *= --val;
  6. }
  7. return ret;
  8. }

 

  1. template <typename T>
  2. T fact(T val) {
  3. if (val == 0 || val == 1)return 1;
  4. T ret = val;
  5. while (val > 1) {
  6. ret *= --val;
  7. }
  8. return ret;
  9. }
  10. int main()
  11. {
  12. int num1;
  13. cout << "Please input a number to calculate the factorial : ";
  14. cin >> num1;
  15. cout << "val\'s factorial is " << fact(num1) << endl;
  16. }

 

  1. template <typename T>
  2. T absolute(T& val) {
  3. return val > 0 ? val : -val;
  4. }
  5. int main()
  6. {
  7. int num1;
  8. cin >> num1;
  9. cout << absolute(num1) << endl;
  10. }

 

形参:在函数的形参列表中声明的局部变量。

局部变量:形参和函数体体内部定义的变量。

局部静态变量:局部变量的生命周期贯穿函数调用及之后的时间。

  1. template <typename T>
  2. T absolute(T& val) {
  3. T ret= val > 0 ? val : -val;
  4. static size_t count=0;
  5. cout << ++count<<" ";
  6. return ret;
  7. }
  8. int main()
  9. {
  10. int num1;
  11. cin >> num1;
  12. for(auto i=0;i<10;i++)
  13. cout << absolute(num1) << endl;
  14. }

 

  1. size_t count() {
  2. static size_t count=0;
  3. return count++;
  4. }
  5. int main()
  6. {
  7. for(auto i=0;i<10;i++)
  8. cout << count() << endl;
  9. }

 

  1. #ifndef CHAPTER6_H_
  2. #define CHAPTER6_H_
  3. template <typename T>
  4. T fact(T val);
  5. template <typename T>
  6. T absolute(T& val);
  7. #endif // !CHAPTER6_H_

 

fact.cpp

  1. #include"Chapter6.h"
  2. #include <iostream>
  3. using namespace std;
  4. int fact(int val)
  5. {
  6. if (val == 0 || val == 1)return 1;
  7. int ret = val;
  8. while (val > 1) {
  9. ret *= --val;
  10. }
  11. return ret;
  12. }
  13. int absolute(int& val)
  14. {
  15. return val > 0 ? val : -val;
  16. }

 

factMain.cpp

  1. #include <iostream>
  2. #include"Chapter6.h"
  3.  
  4. using std::cin;
  5. using std::cout;
  6. using std::endl;
  7. int main()
  8. {
  9. int num;
  10. cin >> num;
  11. cout << fact(absolute(num));
  12. }
  1. cl /EHsc -c factMain.cpp
  2. cl /EHsc -c fact.cpp
  3. cl /EHsc factMain.obj fact.obj

 

  1. void swap(int* a, int* b) {
  2. int c = *b;
  3. * b = *a;
  4. *a = c;
  5. }
  6. int main()
  7. {
  8. int num1,num2;
  9. cin >> num1>>num2;
  10. swap(&num1,&num2);
  11. cout << num1 << " " << num2;
  12. }

 

  1. #include <iostream>
  2.  
  3. using std::cin;
  4. using std::cout;
  5. using std::endl;
  6. template<typename T>
  7. void reset(T& a) {
  8. a = 0;
  9. }
  10. int main()
  11. {
  12. int num;
  13. cin >> num;
  14. reset(num);
  15. cout << num;
  16. }

 

  1. void swap(int& a, int& b) {
  2. int c = b;
  3. b = a;
  4. a = c;
  5. }
  6. int main()
  7. {
  8. int num1,num2;
  9. cin >> num1>>num2;
  10. swap(num1,num2);
  11. cout << num1 << " " << num2;
  12. }

使用引用比使用指针更易于使用,因为代码可读性更高

 

void f(T) 实参被值传递,void f(&T)实参被引用传递,当初始化一个非引用类型的变量时,初始值被拷贝给变量。

 

前文中的swap函数中,参数就应该使用引用。find_char函数张参数c就不应该使用引用。

 

因为s字符串是不能被函数所修改的,所以是常量引用。c可能是一个临时变量,所以不需要使用引用类型,也无需函数加以修改其本身。 如果令occurs为常量引用,则输出occurs为0(不能加以修改),而s可能会在程序中被修改。

 

字符串s在函数中无需修改,所以最好是加上const 表示常量引用。

 

判断是否含有大写字母

  1. bool check_string_upper(const string& s) {
  2. for (const auto& c : s) {
  3. if (isupper(c))
  4. return true;
  5. }
  6. return false;
  7. }

将string对象改成小写

  1. void string_to_lower(string& s) {
  2. for (auto& c : s) {
  3. if (isupper(c))
  4. tolower(c);
  5. }
  6. }

不相同。字符串s在在第一个函数中无需修改,所以最好是加上const 表示常量引用。但是第二个函数要对字符串进行修改

 

  1. bool compare(const matrix&,const matrix&)

比较两个matrix类对象是否相等

  1. vector<int>::iterator change_val(int,vector<int>::iterator);

修改迭代器对应位置的值为i

 

(a):实参数量过多

(b):合法

(c):合法

(d):合法

 

引用形参不能被函数所修改时应该是常量引用。若此时将其设为普通引用那么不能将const对象、字面值或者需要类型转换的对象传递给普通的引用形参,假如其它函数正确的将它们的形参定义为常量引用那么就无法在此类函数中正确使用。

 

  1. int compare(int v1,int*v2) {
  2. return v1 > * v2 ? v1 : *v2;
  3. }

指针类型是int*

 

  1. void swap(int* &v1,int* &v2) {
  2. int* temp = v2;
  3. v2 = v1;
  4. v1 = temp;
  5. }

 

  1. template <typename T>
  2. void print(const T* beg, const T* end) {
  3. while (beg!=end)
  4. {
  5. cout << *beg++ << endl;
  6. }
  7. }
  8. template <typename T>
  9. void print(const T& val) {
  10. cout << val << endl;
  11. }

 

ia声明为具有10个整数的数组,遍历该数组并输入存放的值。数组做参数时,会退化为指针,因此函数中的const int a[10]等同于const int *a,并且长度是不确定的,传a[3]或a[255]是没有区别的。因此如果我们要确定传递一个长度为10的数组,应该这样定义:void print (const int (&a)[10]);

 

  1. int main(int argc, char* argv[])
  2. {
  3. string str;
  4. for (auto i = 0; i != argc; i++){
  5. str += string(argv[i]);
  6. }
  7. cout << str << endl;
  8. }

 

  1. int main(int argc, char* argv[])
  2. {
  3. string str;
  4. for (auto i = 0; i != argc; i++){
  5. str += string(argv[i]);
  6. }
  7. cout << str << endl;
  8. }

 

  1. int list_total(initializer_list<int> list) {
  2. int sum = 0;
  3. for (auto beg = list.begin(); beg != list.end(); ++beg) {
  4. sum += *beg;
  5. }
  6. return sum;
  7. }
  8. int main(int argc, char* argv[])
  9. {
  10. cout << list_total({ 0,10,21,34 });
  11. }

 

const string&类型

 

当循环控制变量是基本类型时,可以不声明为引用,否则应该声明成引用,因为initializer_list对象可能是各种类型,有可能是自定义类型或者string类型。此时使用引用可以避免拷贝。

 

编译器检测出错误1 “str_subrange”: 函数必须返回值,没有检测出错误2 

 

返回局部引用时无效,返回局部定义的常量引用无效。要确保返回的引用有效,就要确定引用所返回的是在函数之前已经存在的某个对象。

 

合法,函数的功能是返回对应整型指针偏移几个地址中所存的值,这在本文中指遍历具有十个整型的整型数组,并把对应下标当做值赋给数组

 

  1. void print_vector(vector<int>::const_iterator it, vector<int>::const_iterator end) {
  2. if (it != end) {
  3. cout << *(it++)<<" ";
  4. print_vector(it, end);
  5. }
  6. }
  7. int main(int argc, char* argv[])
  8. {
  9. vector<int> v{ 0,1,2,3,4 };
  10. print_vector(v.begin(), v.end());
  11. }

 

不发生变化

 

val — 返回的是val的值,相当于又把val当作参数传递,递归将永远不会停止,并且第一次递归不断重复执行。

 

  1. string(&func(string(&strs)[10]))[10];

 

类型别名

  1. using strT = string[10];
  2. strT(&func(strT&strs));

 

尾置返回类型

  1. auto func(string(&strs)[10])->string(&)[10];

 

decltype关键字

  1. decltype(str) (&func(decltype(str)(&strs)));

我觉得decltype 关键字更好,更直观

 

  1. decltype(odd) &arrPtr (int i)
  2. {
  3. return (i % 2) ? odd : even;
  4. }

 

(a)重复声明了int cal(int,int),编译器可以通过实参是否是常量来推测应该调用哪个函数

(b)错误,遇上一个函数相比只有返回类型不同

(c)新函数,参数类型不同属于重载

 

(b)是错误的,因为默认形参右侧的所有形参都必须有默认值

 

(a)非法

(c)合法,但是因为\’*\’是char型,会被隐式转换成int,然后作为wd的值传递给函数

 

  1. string make_plural(size_t ctr, const string& word, const string& ending = "s")
  2. {
  3. return (ctr > 1) ? word + ending : word;
  4. }
  5. int main(void)
  6. {
  7. string str1 = "success";
  8. string str2 = "failure";
  9. cout << make_plural(1, str1,"es") << endl;
  10. cout << make_plural(2, str1,"es") << endl;
  11. cout << make_plural(1, str2) << endl;
  12. cout << make_plural(2, str2) << endl;
  13. }

 

我会把(a)放在头文件里,把(b)放在源文件中,因为inline在编译阶段内联展开

 

  1. inline bool isShorter(const string& s1, const string& s2) {
  2. return s1.size() < s2.size();
  3. }

 

练习题中的函数短小的,没有循环以及递归的应该被定义成内联函数。改写为内联函数只需要在函数声明前加inline关键字就可以。

 

函数“isShorter”不能生成常量表达式,因为isShorter函数中传入的参数不是字面值类型,str1.size() < str2.size()返回的也不是字面值类型。

 

  1. void print_vector(vector<int>::const_iterator it, vector<int>::const_iterator end) {
  2. #ifndef NDEBUG
  3. cout << "vector\'s size is " << end-it << endl;
  4. #endif // NDEBUG
  5. if (it != end) {
  6. cout << *(it++) << " ";
  7. print_vector(it, end);
  8. }
  9. }
  10. int main(int argc, char* argv[])
  11. {
  12. vector<int> v{ 0,1,2,3,4 };
  13. print_vector(v.begin(), v.end());
  14. }

 

不合理,函数的意义是让用户进行输入,直到输入的字符串是sought是停止。因此assert (cin)一直为真,这条语句也就没有意义。可以改为:assert ( s == sought)

 

候选函数:函数匹配的第一步是选定调用对应的重载函数集,集合中的函数称为候选函数

可行函数:从第二步考察本次调用提供的实参,然后从候选函数中选出能被这组实参调用的函数

 

(a) f (2.56, 42) // 非法,因为实参类型是double, int,没有可匹配的函数。如果不是重载函数,只有一个声明f(double, double),则该语句合法。只有在重载时时非法的,要严格执行参数类型匹配。

(b) f (42) // 调用 f (int)

(c) f (42, 0) // 调用 f (int, int)

(d) f (2.56, 3.14) // 调用 f (double, double = 3.14)

 

 

(a)整型提升

(b)算数转换

 

(a)合法,编译器可以通过实参是否是常量来推测应该调用哪个函数

(b)合法,编译器可以通过实参是否是常量来推测应该调用哪个函数

(c)非法,与第一行含义相同,属于重复定义。

 

  1. int func(int a, int b);
  2. typedef decltype(func)* func2;
  3. vector<func2> fvec;

 

  1. int func(int a, int b);
  2. typedef decltype(func)* func2;
  3. vector<func2> fvec;
  4. inline int add(int a, int b) {
  5. return a + b;
  6. }
  7. inline int subtract(int a, int b) {
  8. return a - b;
  9. }
  10. inline int multiply(int a, int b) {
  11. return a * b;
  12. }
  13. inline int divide(int a, int b) {
  14. return static_cast<double>(a) / b;
  15. }
  16. int main(int argc, char* argv[])
  17. {
  18. fvec.push_back(add);
  19. fvec.push_back(subtract);
  20. fvec.push_back(multiply);
  21. fvec.push_back(divide);
  22. }

 

  1. int func(int a, int b);
  2. typedef decltype(func)* func2;
  3. vector<func2> fvec;
  4. inline int add(int a, int b) {
  5. return a + b;
  6. }
  7. inline int subtract(int a, int b) {
  8. return a - b;
  9. }
  10. inline int multiply(int a, int b) {
  11. return a * b;
  12. }
  13. inline int divide(int a, int b) {
  14. return static_cast<double>(a) / b;
  15. }
  16. int main(int argc, char* argv[])
  17. {
  18. fvec.push_back(add);
  19. fvec.push_back(subtract);
  20. fvec.push_back(multiply);
  21. fvec.push_back(divide);
  22. int a = 10, b = 5;
  23. for (auto& i : fvec) {
  24. cout << i(a, b)<<" ";
  25. }
  26. }

 

版权声明:本文为GodZhuan原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/GodZhuan/p/13888002.html