chapter_6_

1. 函數的返回類型不能是數組或者函數,但是可以是指向數組或者函數的指針

2. 實參和形參區別:

形參:函數定義時的參數,無賦值的參數,用來說明參數類型

實參:函數調用時的參數,賦值的參數,函數實際操作的對象

3. 形參、局部變量、局部靜態變量的區別

局部變量(local variable):函數體內部定義的變量和形參統稱爲局部變量

局部靜態變量(local static variable):直到程序結束時才銷燬的對象,不受塊的約束

4. 練習6.7,編寫函數,第一次被調用時返回0,以後每次被調用返回值加1。

#include<iostream>
using namespace std;

int T(){
    static int count = -1;
    count++;
    return count;
}
int main(){
    int ret;
    for(int i=0; i<5; i++){
        ret = T();
        cout<<ret<<endl;
    }      
}

5.參數傳遞:引用傳遞、值傳遞、指針傳遞

引用傳遞:形參是引用類型,它將被綁定到對應的實參;

值傳遞:將實參的值拷貝後賦給形參(包括指針),對變量的改動不會影響初始值

指針傳遞:指針的行爲同其它非引用類型一樣,當執行指針拷貝操作時,拷貝的是指針的值,即其地址

避免使用引用的過程中拷貝,因爲如果對象特別巨大,拷貝的過程會非常緩慢,造成程序低效

使用引用傳遞可以避免拷貝,有的類型並不支持拷貝操作,只能通過引用形參進行訪問;當函數無須修改引用形參的值時,最好使用常量引用。如:

bool isShorter(const string &s1, const string &s2){
    return s1.size() < s2.size();
}

練習6_10:使用指針形參交換兩個整數的值

#include<iostream>
using namespace std;
//練習6.10
void exchange_pointer(int *n1, int *n2){
    int temp = *n1;
    *n1 = *n2;
    *n2 = temp;
}
void exchange_value(int &n3, int &n4){
    int temp = n3;
    n3 = n4;
    n4 = temp;
}
int main(){
    int n1 = 10, n2 = 20;
    cout<<"值傳遞交換前的地址和值:"<<endl;
    cout<<"&n1:"<<&n1<<"  n1:"<<n1<<endl;
    cout<<"&n2:"<<&n2<<"  n2:"<<n2<<endl;
    exchange_pointer(&n1, &n2);
    cout<<"交換後的地址和值:"<<endl;
    cout<<"&n1:"<<&n1<<"  n1:"<<n1<<endl;
    cout<<"&n2:"<<&n2<<"  n2:"<<n2<<endl;

    int n3 = 30, n4 = 40;
    cout<<"引用傳遞交換前的地址和值:"<<endl;
    cout<<"&n3:"<<&n3<<"  n3:"<<n3<<endl;
    cout<<"&n4:"<<&n4<<"  n4:"<<n4<<endl;
    exchange_value(n3, n4);
    cout<<"交換後的地址和值:"<<endl;
    cout<<"&n3:"<<&n3<<"  n3:"<<n3<<endl;
    cout<<"&n4:"<<&n4<<"  n4:"<<n4<<endl;
    return 0;    
}
/*
指針值傳遞,可以看到雖然交換了n1和n2的數值,但是n1和n2的地址並沒有改變
值傳遞交換前的地址和值:
&n1:0x7ffd5472b8fc  n1:10
&n2:0x7ffd5472b8f8  n2:20
交換後的地址和值:
&n1:0x7ffd5472b8fc  n1:20
&n2:0x7ffd5472b8f8  n2:10

引用傳遞交換前的地址和值:
&n3:0x7ffd5472b8f4  n3:30
&n4:0x7ffd5472b8f0  n4:40
交換後的地址和值:
&n3:0x7ffd5472b8f4  n3:40
&n4:0x7ffd5472b8f0  n4:30
*/

6. const引用

常量引用:當形參的頂層爲const,接收常量對象或者非常量對象均可

非常量引用:無const,只能接收非常量對象,否則編譯報錯

 

7. 6_22,交換兩個int指針,可以和6_10作對比

//6.22 交換兩個int指針
void exchange_int_pointer(int* &n5, int* &n6){
    int *temp = n5;
    n5 = n6;
    n6 = temp;
}

int main(){
    int n5 = 50, n6 = 60;
    int *n5_pointer = &n5;
    int *n6_pointer = &n6;
    cout<<"交換前的地址和值:"<<endl;
    cout<<"n5_pointer:"<<n5_pointer<<"  n5:"<<n5<<"  *n5_pointer:"<<*n5_pointer<<endl;
    cout<<"n6_pointer:"<<n6_pointer<<"  n6:"<<n6<<"  *n6_pointer:"<<*n6_pointer<<endl;
    exchange_int_pointer(n5_pointer, n6_pointer);
    cout<<"交換後的地址和值:"<<endl;
    cout<<"n5_pointer:"<<n5_pointer<<"  n5:"<<n5<<"  *n5_pointer:"<<*n5_pointer<<endl;
    cout<<"n6_pointer:"<<n6_pointer<<"  n6:"<<n6<<"  *n6_pointer:"<<*n6_pointer<<endl;

    return 0;    
}

/*
可以看到交換之後n5,n6的值沒變,但是n5_pointer和n6_pointer的值及其指向的對象發生了改變
交換前的地址和值:
n5_pointer:0x7fff7609bee8  n5:50  *n5_pointer:50
n6_pointer:0x7fff7609bee4  n6:60  *n6_pointer:60
交換後的地址和值:
n5_pointer:0x7fff7609bee4  n5:50  *n5_pointer:60
n6_pointer:0x7fff7609bee8  n6:60  *n6_pointer:50
*/
 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章