关于我年久失修的C++的康复记录8

Chapter 8 Function Advance

0.Before Everything

这是一份四年没有学过C++的菜鸡由于公司业务需求重拾C++的复习记录,因为工作需要在练习英文,加上Ubuntu下输入法与IDE的冲突,所以写纯英文的笔记,顺便练习一下自己英文的水平,如果有表达不当或理解错误,还请看到的大佬提醒我,共勉,谢谢。

8.1 Inline Function

1. What is inline function

Inline function is an improvement measure in C++ for improving execute speed of program. The main differences between
normal function and inline function is how the compiler compositing it into the program.

How does an inline function improve the execution speed?

A: Replacing the function call with its implementation.

In a normal program with function call, current function will be suspended at the point of
a function call. This function call refers to an address of another function.
This address will be pushed to stack.

While using an inline function, the calling of inline function will be replaced by its
implementation after compiling. There will not be any function call while executing
program.

2.How to use inline function

Declaration:

inline double square(double x) { return x * x;}

int main(){
double x = square(1.5);
std::cout << x << std::endl;
}

Using inline to declare a function as inline function

Call an inline function like normal function while coding. Compiler will replace the
function call with its implementation while compiling.

3.Inline function and macros

Inline function is a new feature for c++ to replace marcos which works as a function
and solve some potential problems. Marco is the origin format of inline function.

#define SQUARE(X) X*X; // define a marco

a = SQUARE(5.0); // is replaced by 5.0*5.0
b = SQUARE(3.5+4.5); // is replaced by 3.5 + 4.5 * 3.5 + 4.5 

Only first statement can work correctly. And this marco can be improved by using brackets:

#define SQUARE(X) ((X)*(X))

8.2 References Variable

1.What is reference variable

References variable is a new compound type in C++. It works like an alias or an alternative name.
The main use for reference variable is as a formal argument to a function.

2.Create a reference variable.

    int rats;
    int & var = rats; // make var an alias for rats.
    int * req = &rats; //req is a pointer to rats. 

int& indicates that var is a reference variable points to rats. Both req and var point to the rats.
Reference looks very similar with pointer, and there are some differences. For example, the expression of
them are different. And, reference has to been initialized when declare and can not be re-assign.

This feature makes reference like a const pointer. Which means int & ptr = rats equals to int * const ptr = rats

3.Pass reference as an argument

Reference usually works as an argument for a function. This makes the variable in this function as a alias of the origin variable.
This form is called reference passes. The function which is called can access the variable in calling function.

Talk about three methods to swap values of two variable by operating reference:

  1. void swap(int & a, int & b);
  2. void swap(int * p, int * q);
  3. void swap(int x, int y);

The first method uses references to swap two ints:

void swap(int & a, int & b){
    int temp = a;
    a = b;
    b = temp;
}

And second method uses pointer:

void swap(int * p, int * p){
    int temp = *p;
    *p = *q;
    *q = temp;
}

These two methods can swap two values successfully. Third method swap two variable directly:

void swap(int a, int b){
    int temp = a;
    a = b;
    b = temp;
}

The third method can not swap two values as it’s expected. Because the value passes by formal
parameters a and b is two copy of the origin variables. While the first two methods operates
the variable by pointer or reference can change the value of the values in calling function.

4.Situations that should use reference parameter

When the parameters are:

  1. small data type like inner or small structure, use value passes.
  2. array, only use const pointer.
  3. big structure, using const pointer or const reference.
  4. object, only use const reference.
  5. inner data type, use pointer.

8.3 Default Parameter

1.What is default parameter

The default parameter is a value which is used automatically when an argument is omitted for a
function call. This feature makes the use of function more flexible.

2.How to set a default parameter

The default parameter can be set in the function prototype like an initialization to a variable.

void func(int i = 1);

func() takes a integer argument with default 1.

For a function with multiple arguments, the default value setting must start from left to right

int func1(int i, int j = 1, int k = 0); // valid
int func2(int i, int j = 0; int k); // invalid

8.4 Function Overload

1.Overload and polymorphism

Function overload is an implement form of function polymorphism which is added in C++. Overload means some function
with same name take different arguments and do different things.

2.Provide an overload for a function

void func(int i); // the origin function
void func(string str); // one overload function of `func`

The key point of function overload is the parameter list, also named function signature. One function holds three signature
to be distinguished with others: function name and parameter list. The parameter list can be different on quantity, sequence
and type. And one of these three is different will be distinguished as two function.

Function reload requires that all functions have a same name and different parameter list.

void print(const char * str, int width); // #1
void print(double d, int width); // #2 

Function 1 and 2 have a same name and different parameter list, so that they are two “overload”
of function print().

8.5 Template Function

1.What is function template

Template function is a new feature in new C++ compiler. It supports us to define a function with
generic parameter. As the type is shown by parameter, this feature also named parameterized types.

2.Declaration of template function

template<typename T> // see also template<class T>
void swap(T &a, T &b){
    T temp;
    temp = a;
    a = b;
    b = temp;
}

This declaration means the function swap receives two parameters. These parameters are type T, and T
will be recognized while compiling and replaced by the target type.

3.Limitations of template

All the operations in a template function must support all types it may receive. Assuming that there is
an operation a > b on template variable T a and T b, the type T must support operator ‘>’.
If both a and b were two structures or class which can not be compared by ‘>’, this declaration will not
be allowed. Compiler will check this error before compiling.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值