Article Directory
- Chapter VI Functions
- return value
- Automatic object
- Local static objects
- Function declarations
- Pointer parameter
- A reference parameter
- const parameter
- To parameter assignment
- Constant reference parameter
- Array parameter
- variable parameter
- Ellipses parameter
- return
- Returns an array of pointers
- Overload
- Overloading and Scope
- Default argument
- Call the default function arguments
- The default argument declaration
- The default value of the initial argument
- Inline function improve performance _
- constexpr function _ to improve performance
- Function pointer
Chapter VI Functions
return value
And the function can not be an array
Automatic object
{} Is present in the life cycle of the object
Local static objects
Performed only once, have been effective;
Built-in type of local static variables are initialized to 0;
fun(){
static int i=0;
}
Function declarations
void fun(int,int);
Declare functions and variables in header files, functions and variables defined in the source file.
[!] Source file to include the header file
Pointer parameter
fun(int *p){}//传递的是指向的对象的地址,不是指针的地址
[!]建议用int &来代替指针,避免拷贝,只读用const int &
A reference parameter
Additional information can be returned
const parameter
Participants ignore non-const-shaped top const
void fun(const int i);//只读,可以接受常量和非常量
error writing
void fun(const int i);//只读
void fun(int i);//只读
To parameter assignment
Initialize variables analogy
const int &a=42;//字面值可以给const int &
int &a=42;//error字面值不可以给int &
int &i=a;//error,const int &不可以给int &,但是可以给int
Constant reference parameter
When the same parameter, const reference declared as possible, because the common reference parameter can not receive the following types {const object literal object types require conversion}
//如果内层确实无法使用常量引用就创建i的副本i2。
fun1(const int &i){
int i2=i;
fun2(i2){
//fun2 的形参是int&
}
}
Array parameter
Passed to the function as a pointer
Statement form
fun(int*);
fun(int[]);
fun(int[10]);
Array size
//传递头尾指针
fun(int*beg,int*en);
//使用标准库
int arr[2]={1,2};
fun(begin(arr),end(arr));
//////////////////////////////////
//字符串,'\0'结束
fun2(char*cp){
if(cp)
while(*cp){*cp++;}
}
//////////////////////////////////
//传递length
Array reference parameter
f(int (&arr)[10]){...}
f(int &arr[10]){...};//error,数组里面有10个引用
Multidimensional arrays of pointers
//二维[10][10]
void print(int (*p)[10],int row){...}
//等价定义
void print(int p[][10],int row){...}
variable parameter
Transmitting the same argument types: initializer_list library type
Different types of arguments: variadic templates
For C functions and interactions: apostrophe
initializer_list<T> lst;//元素是常量
initializer_list<T> lst{a,b,c};
lst2(lst);
lst2=lst;
lst.size();
lst.begin();//auto beg=lst.begin()..遍历
lst.end();
Passing arguments sequences {}
+ Variable parameters General parameters
//声明
void fun(ErrCode e,initializer_list<string> lst);
//传递实参
fun(ErrCode(0),{"dwa","dwa"})
Ellipses parameter
C ++ and C code convergence
The code uses the varargs c standard library functions,
... can only appear in the final
void foo(para_list,...);
void foo(...);
return
void
return;
//可以返回另一个void fun2()
return fun2();
Can return a reference, do not copy the
Returns the local variable (including literals), the pointer will not. Function ends, they do not exist.
Return the object to be fun (). Members
Return pointer to be fun () -> mem
Returns a reference, it can be left value
{,,} returns a list may be empty
Returns an array of pointers
Method One: Alias
//arr是含有10个整数的数组
typedef int arr[10];
//等价写法
using arr =int[10];
//函数声明
arr*fun(int i);//返回值:arr的指针
Method Two: statement
int arr[10];
int *p[10];//p是数组,里面元素是指针
int(*p2)[10]=&arr;//指向含有10个整数的数组
int (*fun(int i))[10];//返回值类型,数组int[10]的指针
/*
解读:*fun(int i)函数结果解引用,(*fun(int i))[10]是个数组,int (*fun(int i))[10]数组里面是int
*/
Method three:
auto fun(int i)->int(*)[10]
Method four:
int arr[10];
decltype(arr) * arrfun(int i){return &arr;}
Overload
The procedure for the argument, the function calls the same. Different parameter list.
The parameter is a pointer or reference
Constant parameter is divided into objects and objects const
It is the underlying const
fun(const int&);//接收常量和非常量,非常量可以转化成常量,反之不行
fun(int &);//非常量优先选择这个
fun(const int*);//接收常量和非常量,非常量可以转化成常量,反之不行
fun(int *);//非常量优先选择这个
const_cast and overloading
const_cast cast is used
now use const return value is a function, the return value is non-const function construct
const int &fun(const int &i){return i;}
int &fun(int &i){
auto &r=fun(const_cast<const int&>(i));//向const转型
return const_cast<int&>(i);}//再转回int
Overloading and Scope
Statement name (including the variables and functions of the same name), the inner layer entity will overwrite the entity with the same name
Default argument
Given a default parameter value, the subsequent have default values
Call the default function arguments
The default parameters may be omitted tail solid
The default argument declaration
It may be declared more than once, but twice declared, with a default value of the argument to be the same.
And a parameter default values given, the subsequent have default values. The default value has been declared can not write, you can write only numeric types such as int
The default value of the initial argument
Local variables can not be used as a default argument
return value of the function can do default argument
int i=1;
int j=2;
int getnum();
void fun(int =getnum(),int=i,int=j);
fun2(){
i=2;//修改了默认实参i
int j=3;//覆盖外部j,但是局部变量不能作为默认实参
//默认实参j还是2
}
Inline function improve performance _
Simple, frequent calls, direct flow function, replace front body of the function return value plus inline, the function call can compile time, instead of calling the function
constexpr function _ to improve performance
Member function calls replace function
is implicit inline function
returns the value of a function parameter is to be literal type,
the return value can be a constant amount may be very
constexpr int fun1(){return 33;}
constexpr int a=fun1();
constexpr inline functions and functions defined in the header file
Function pointer
Pointer to replace the function name
int fun(int i);
//函数指针
int (*p)(int);
//两种定义方法
p=fun;
p=&fun;
//调用的两种方法
p(1);
(*p)(1);
//函数形参是函数指针的两种声明方法
fun2(int (*p)(int));//可以接收fun函数的指针
fun2(int p(int));
//调用fun2
fun2(fun);//函数名fun自动转换为指针
Parameter using aliases
/* 别名_函数类型 */
//取函数fun的类型
typedef int Nname(int i);//Nname是fun的函数类型
//取函数fun的类型的等价形式
typedef decltype(fun) Nname2;//Nname2是fun的函数类型
/* 别名_函数指针 */
typedef int (*pname)(int i);//pname是fun的函数指针
//取函数fun的指针的等价形式
typedef decltype(fun) *pname2;//pname2是fun的函数指针
/* 别名_使用 */
//两种方法,fun4的声明
fun4(Nname);//函数类型自动转换为函数指针
fun4(*pname);
The return value using an alias
/*返回值是函数指针*/
//类型别名
using F=int(int);//fun函数类型
using PF=int(*)(int);//fun函数指针
//返回值是函数指针的函数声明的两种方式
PF func(int);
F *func2(int);//不会函数类型自动转换为函数指针
//所以要加上*
Aliases do not declare the return value directly
method one
int fun(int i);
//返回值为指向fun类型函数的指针
int (*func3(int))(int);
//func3(int)是函数声明,
//*返回指针,(*func3(int))(int)最后一个(int)是指针指向函数的形参列表
//第一个int是指针指向函数的返回值类型
Method two: Tail set return value declared
int fun(int i);
auto fun3(int)->int(*)(int);//返回值为指向fun类型函数的指针
Method three: decltype
decltype(fun) *fun5(int);//返回值为指向fun类型函数的指针
//注意:decltype返回函数类型,不返回指针