Pass-by-value parameter When the function is executed, the actual parameter is assigned to the formal parameter. The copy process is completed by the copy constructor of the formal parameter type . If the actual parameter type is different, the type conversion will be performed. The actual parameter is converted to the shape parameter type (conversion is allowed in the case of). The destructor of the parameter type is responsible for releasing the parameter when the function runs. Therefore, the function call does not change the value of the actual parameter corresponding to the formal parameter.
Reference parameter The actual parameter is the alias of the formal parameter. The program does not copy the value of the actual parameter when the function is called, and the destructor is not called when the function returns.
Constant reference parameters Use the keyword const to indicate the unmodifiable reference parameters of the function, and tell the user that the function will not modify the actual parameters.
template<classT>//模板函数
T abc1(T a, T b, T c){
return a + b * c;}template<classT>//引用参数
T abc2(T &a, T &b, T &c){
return a + b * c;}template<classT1,classT2,classT3>//常量引用参数
T1 abc(const T1 &a,const T2 &b,const T3 &c){
return a + b * c;}
Return value A function can return a value, a reference, or a constant reference. Adding a suffix & to the function return type specifies a reference return . This form of return does not copy the value of z to the return environment. When the function ends, the space of the formal parameter i and all local variables is released, and z is only a reference to the actual parameter and is not affected. If you add the const keyword, you get a const reference return, and the const reference return must be assigned to a const constant when it returns to the calling environment.
template<classT>
T&mystery(int i, T &z){
return z
}
Throwing exceptions and handling exceptions The base class of exceptions is exception. There can be one or more catch blocks after the try block. If an exception is not thrown, the normal operation of the try block will stop, and the program will enter and execute the first catch block that catches this exception type, and other catch blocks will be ignored. If it is not caught by any catch block, the program stops abnormally.
intabc4(int a,int b,int c){
if(a <=0|| b <=0|| c <=0)throw"All parameters should be > 0";//抛出异常return a + b * c;}intmain(){
try{
cout <<abc4(2,0,4)<< endl;}catch(char*e){
//捕捉char*类型异常
cout << e << endl;return1;}return0;}
The dynamic storage space allocation C++ operator new is used for dynamic storage allocation or runtime storage allocation. Its value is a pointer to the allocated space. delete is used to release the memory allocated by new. A one-dimensional array of dynamic allocation can be created as follows. The operator new allocates space for n integers and returns a pointer to the first integer space. If the computer does not have enough memory to allocate, new will not allocate memory and throw an exception of type bad_alloc, which can be caught by the try-catch structure.
int*x=newint[n];
int n;try{
int*x =newint[n];}catch(bad_alloc){
cout <<"out of memory"<< endl;exit(1);}
delete y;//用于释放*ydelete[]y;//用于释放一维数组y
Dynamic allocation of two-dimensional array If the two-dimensional array column or row position, use new dynamic allocation to create. If both are unknown at compile time, it is impossible to call new once to create a two-dimensional array. When created, it can be seen as a structure composed of several rows, and each row is a one-dimensional array that can be created with new. The pointer to each row is stored in another one-dimensional array. The release of the two-dimensional array also requires steps. The three codes are as follows.
char(*c)[5];try{
c =newchar[n][5];}catch(bad_alloc){
cerr <<"out of memory"<< endl;exit(1);}
char**x;//x是指向指针的指针 try{
x =newchar*[n];//创建行指针for(int i =0; i < n; i++)//为每一行分配空间
x[i]=newchar[m];returntrue;}catch(bad_alloc){
returnfalse;}
for(int i =0; i < n; i++)//释放每一行数组空间delete[]x[i];delete[]x;//释放行指针
x =NULL;