chapter3---C++函数模板
一,【前言】
对函数和函数模板你有多少了解,尤其是函数模板.在面试对象程序设计中, 成员函数也是函数, 正确的设计函数原型和参数类型,不仅仅能保证函数的正确性,而且能提高程序设计的效率.
本博文主要对默认函数, 内联函数, 函数联编, 函数重载, 函数多态性和函数模板进行学习和总结:
二,【详情】
1,语言的鸿沟是什么?
解答: 自然语言 与机器语言的差距叫语言的鸿沟.
自然语言,就是人能明白, 人类用的语言.
2,返回引用: int&index(int i);
返回对象: string str=input(n);
返回指针: float *input(int &n);
3,值传递和引用传递都是什么?有什么特性?
解答: (1)值传递,在C++ 中一般是 用指针(传递地址值), 有变量值和地址值, 多是形成, 实参是对象地址---值传递.
(2)引用(地址), 引用形参(别名, 改变参数也是改对象的值, 引用传递.)
传递对象地址值是使用对象指针作为参数; 传递地址是使用对象引用作为参数.
Pass-by-value:
The actual parameter (or argument expression) is fully evaluated and theresulting value is copied into a location being used to hold the formalparameter's value during method/function execution. That location is typicallya chunk of memory on the runtime stack for the application (which is how Javahandles it), but other languages could choose parameter storage differently.
Pass-by-reference:
The formal parameter merely acts as an alias for the actual parameter. Anytimethe method/function uses the formal parameter (for reading or writing), it isactually using the actual parameter.
从上面我们可以看到, 无论是值传递还是引用传递,都会改变原来对象的值, 这怎么办? 我们可以用const常量来进行保护:
#include<iostream>
#include<string>
Usingnamespace std;
Voidchange(const string&);
Voidmain(){
string str(“Can you changt it ?”);
change(str);
cout<< str<< endl;
}
Void change(conststring &s)
{
string s2=s+”No”;
cout<< s2<<endl;
}
输出结果如下:
Can youchange it? No!
Can youchange it ?
5,默认函数赋值:
(1),赋值默认参数: 从左往右
(2),实参赋值: 从右--->左
6,内联函数是什么?为什么要有内联函数?
解答: 内联函数, 通俗的说就是函数中的函数.
有关键字: inline
7, 内联函数什么时候用?
解答:代码少, 小的时候
8, 使用内联函数的目的?
解答: 1,不浪费空间; 2,加快程序执行速度.
9,什么是函数的动态联编和静态联编?
三,【小结】
本博文主要是对C++函数和函数模板中的一些基本语法, 概念进行小结,梳理和总结, 主要对默认函数, 内联函数, 函数联编, 函数重载, 函数多态性和函数模板进行学习和总结.