First, inline function 1, advantages:
An inline function is a relatively provincial resource, and it is generally more appropriate to use a short, high-frequency function as an inline function.
2, a demo
" stdafx.h " #include<iostream>usingnamespacedouble Square (double return x*x;} int _tmain (int argc, _tchar* argv[]) { int; << Square (334);}
Second, reference variable (alias) 1, create reference variable
C and C + + use the "&" symbol to indicate the address of the indicated variable. But "&" in C + + also represents a declaration reference
int Rat; int © = rat;
When declaring a reference variable, you must initialize it and be sure to do so.
2. Use references as parameters to functions
Directly associates an argument with a formal parameter, which is similar to a different name for the same person. So the modification of the formal parameter causes the argument to change as well, because the two are one.
3. Use const whenever possible
4. Apply references to Structures
//Exercise.cpp: Defines the entry point of the console application. //#include"stdafx.h"#include<iostream>using namespacestd;structsysop{Charname[ -]; Charquote[ -]; intused;};ConstSysop & Use (sysop &sysopref);int_tmain (intARGC, _tchar*argv[]) {sysop looper= { "Rick \ "fortran\" Lopper", "I ' m a goto kind of guy.", 0 }; Use (Looper); cout<<"Looper:"<< looper.used <<"Use (s) \ n"; Sysop copycat; Copycat=Use (Looper); cout<<"Looper:"<< looper.used <<"Use (s) \ n"; cout<<"Use (looper):"<< use (looper). Used <<"Use (s) \ n";//members of the struct body that can directly use the return value of a function return 0;}ConstSysop & Use (sysop & sysopref)//The return value is a reference that can save time on assignment (no more temporary variables){cout<< Sysopref.name <<Endl; cout<< Sysopref.quote <<Endl; Sysopref.used++; returnSysopref;}
The return value will not be modified if the normal return value is added to the const. For example, the following conditions.
Use (looper). used=;
You should not return a reference to a memory unit that no longer exists when the function terminates, for example
Const Sysop & Clone (sysop & sysopref) { sysop newguy; = sysopref; return Newguy;}
That is, a reference declared within a function cannot be a return value.
Third, the default parameter 1, definition:
The default parameter refers to a value that is automatically used when an argument is omitted from a function call.
2. Setting Method:
Assign a value when declaring a function:
Char *left (constcharint n=1);
Some rules:
(1) The default value must be added from right to left, that is, to set a default value for a parameter, the right parameter must also have a default value set,
(2) The argument must be given a corresponding formal parameter from left to right, and no parameters can be skipped.
Iv. function overloading 1, definition:
The same function name has different function functions.
2, the realization method:
For functions of the same name, it is necessary to differentiate the characteristic quantity, so the characteristic quantity is the number and type of the parameter.
Some rules
(1) The same type of references and non-reference variables are the same as the characteristics.
(2) When matching a function, the const and non-const variables are not distinguished.
(3) The return value type is not a characteristic amount
V. Function template 1, definition:
The same function, just a different type of parameter, so a template for a function is developed.
2. Setting Method:
Template <typename any> //template can be replaced by class
void Swap (Any &a, no &b) {any temp; = A; = B; = temp;}
3. Overloaded templates
Template <class any>void Swap (any &a, any &<class any >void Swap (any a[],any b[],int m);
4. Display materialized
If you define the following structure:
struct job{ char name[]; Double salary; int Floor ;}
If you want to be able to exchange the salary and floor members, instead of exchanging the name members of two variables. We can achieve this by displaying materialization:
(1) Related rules
(2) a demo
void Swap (Job &,job &<class any>void Swap (any &,any &<> Swap<job> (Job &,job &); // Show materialized
C++premer Plus Study (v)--function exploration