C ++ statement function Revisited

1. calculation expression only, discard calculation result;

2. empty statement does nothing;

3.switch case statement leakage write break, will begin to match the situation until the end of the statement

int main () 
{ 
   int I = 0 ; 
   I + 100 ;      // expression 
   ;             // null statement 
   the while (CIN >> I = I &&! 0 ) 
   ;             // empty statement 
   Switch (I) {
        Case  . 1 : 
       COUT < < " . 1 " << endl;
        Case  0 : // begin 
       COUT << " 0 " << endl;
        Case  2 :
       cout << "2" << endl; 
       default:
       cout << "3" << endl;//执行结束
   }
   return 0;
}

4. parameter, arguments, local variables, static local variables

Parameter is called placeholder only and space applications at the beginning of the function, by passing arguments instantiated (also called copy constructor). It is generally localized, i.e. visible only function in vivo.

Arguments used to instantiate / initialization parameter

The scope of local variables is a function of the body or an internal statement, but with static modification becomes a static local variable, its scope or function of the body, but to extend the lifetime of the program is terminated.

int countCalls ( int A, int B) 
{ 
    static  int Calls = 0 ; // static local variable
     return ++ Calls; 
} 

int main () 
{ 
    for ( int I = 0 ; I < . 5 ; I ++ ) 
        COUT << countCalls ( . 1 , 2 ) << endl;
     // COUT Calls << << endl; // can not access the 
    return  0 ; 
}

The function returns a value, or a copy of occurrence, i.e., the amount of call initializing a temporary point function with a variable function in vivo.

6. Do not return a local variable reference or pointer, after the return to the destruction, not to look for the child.

7. Referencing the return value left, the other is the right type of return value.

8. The inline function suitable for optimizing regulatory smaller die, the process directly, frequently called function.

9. interesting macro returns an error message (static variables)

void trouble(int m)
{
    if(m == 0)
    {
        cout << "Error : " << __FILE__;
        cout << " in function  " << __func__;
        cout << " at line " << __LINE__;
        cout << " complie on " << __DATE__;
        cout << " at " << __TIME__;
    }
}

10. The ambiguity function overloading

(1) there is no function parameter VS parameters have default values, ambiguity

int get(){
    return a;
}
int get(int a = 5){
    return a;
}
//调用get()

(2) pass by reference by value VS, ambiguity

int get(int a){
    return a;
}
int get(int &a){
    return a;
}
//int m = 10;
//调用get(m)

The presence of (3) Parameter implicit type conversions, ambiguity

int get(int m){
    return m;
}

long get(long m){
    return m;
} 
//double m = 10.123
//调用get(m)

Reference material

[1] https://blog.csdn.net/chenxiaohua/article/details/2128899  (something wrong)

[2] https://blog.csdn.net/loving_forever_/article/details/51472127  (something wrong)

Guess you like

Origin www.cnblogs.com/yocichen/p/11108670.html