c++-引用

c++中引用類型,引用是如何引用的呢?

  • 引用
    • 引用概念,給變量起個別名,本質是給內存空間取個別名
    • 引用是c++的語法概念、引用的意義(好用)
    • 引用本質:有地址、定義時必須初始化,c++編譯器內部按照指針常量
    • 引用結論:間接賦值成立的三個條件的後兩步和二爲一
    • 引用使用原則:當用引用時,咱們不去關心編譯器引用是怎麼作的;當分析奇怪的語法現象時,纔去考慮c++編譯器是怎麼作的
    • 函數返回值是引用(若返回棧變量,不能成爲其餘引用的初始化)
    • 函數返回值當左值,必須返回一個引用
    • 指針的引用int getT(Teacher * &myp )
    • 常引用
      • 用變量初始化const引用,然變量形參擁有隻讀屬性
      • 用字面量初始化const引用,額外的分配內存空間
  • 引用概念,給變量起個別名,本質是給內存空間取個別名
  • 引用是c++的語法概念、引用的意義(好用)
  • 引用本質:有地址、定義時必須初始化,c++編譯器內部按照指針常量
  • 引用結論:間接賦值成立的三個條件的後兩步和二爲一
  • 引用使用原則:當用引用時,咱們不去關心編譯器引用是怎麼作的;當分析奇怪的語法現象時,纔去考慮c++編譯器是怎麼作的
  • 函數返回值是引用(若返回棧變量,不能成爲其餘引用的初始化)
  • 函數返回值當左值,必須返回一個引用
  • 指針的引用int getT(Teacher * &myp )
  • 常引用
    _ 用變量初始化const引用,然變量形參擁有隻讀屬性
    _ 用字面量初始化const引用,額外的分配內存空間

這與c大有不一樣ios

1 引用沒有定義, 是一種關係型聲明。聲明它和原有某一變量(實體)的關
系。故 而類型與原類型保持一致, 且不分配內存。與被引用的變量有相同的地
址。c++

2 聲明的時候必須初始化, 一經聲明, 不可變動。編程

3 可對引用, 再次引用。屢次引用的結果, 是某一變量具備多個別名。函數

4 & 符號前有數據類型時, 是引用。其它皆爲取地址spa

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>


using namespace std;
/*

*/

void change_value(int *p) // int p = a;
{
    *p = 30;
}

void change_value2(int & r)//int &r = a
{
    r = 30; //a = 30
}


int main(void)
{
    int a = 20;
    int b = 30;


    int *p = &a;
    *p = 30;

    p = &b;
    *p = 20;//b

    int &re = a; //int & 使用引用數據類型, re就是a的別名

    re = 50;
    &re;
    &a;

    re = b; //讓re成爲b的引用?   a = b
    re = 50;
    cout << "a =" <<a << endl;
    cout << "b = " << b << endl;
    int & re2 = b; //引用必定要初始化,

    re2 = a;


    int &re3 = re;

    re3 = 100;
    cout << "a =" << a << endl;
    cout << "re =" << re << endl;
    cout << "re3 =" << re3 << endl;



    cout << "-------------" << endl;
    cout << "a =" << a << endl;
    change_value(&a);//改變了值
    cout << "a =" << a <<  endl;
    a = 100;
    cout << "-------------" << endl;
    cout << "a =" << a << endl;
    change_value2(a);//改變了值
    cout << "a =" << a << endl;

    return 0;
}

引用示例

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>


using namespace std;

struct student
{
    int id;
    char name[64];
};

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

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

void printS(struct student s) //student s = s1; 結構體整個值拷貝的動做
{
    cout << s.id <<" "<< s.name << endl;
    s.id = 100;
}

void printS1(struct student *sp)
{
    cout << sp->id << " " << sp->name << endl;
    sp->id = 100;
}

//引用在必定條件下可以取代指針的工做,也能做爲函數參數。
void printS2(struct student &s)//student &s = s1;
{
    cout << s.id << "  " << s.name << endl;
    s.id = 300;
}

int main(void)
{
    int a = 10;
    int b = 20;

    my_swap2(a, b);
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;


    student s1 = { 10, "zhang3" };

    printS(s1);
    printS1(&s1);
    printS2(s1);

    cout << "si.id =" << s1.id << endl;

    return 0;
}

引用的本質

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>


using namespace std;

struct typeA
{
    int &a;
};

struct typeB
{
    int *a;
};


struct student
{
    int id;
    char name[64];
};

void motify(int *const a)//int *const a = main::&a
{
    *a = 300;
}

void motify2(int &a) //當咱們將引用做爲函數參數傳遞的時候,編譯器,會替咱們將實參,取地址給引用
                    //int &a = main::&a
{
    a = 300; //對一個引用操做 賦值的時候,編譯器提咱們隱藏*操做。
}

//若是咱們在去研究引用的時候,你能夠將引用當作一個常指針去研究
//當你在使用引用編程的時候,你就把引用理解爲變量的別名就能夠了。


int main(void)
{
    cout << "sizeof(struct typeA)" << sizeof(struct typeA) << endl;
    cout << "sizeof(struct typeB)" << sizeof(struct typeB) << endl;
    //引用所佔用的大小 跟指針是相等的。
    int a = 10;
    int &re = a; //常量要初始化,引用也要初始化, 引用多是一剛常量。

    int *const p = &a;
    //綜上兩點, 引用 多是一個常指針。


    motify(&a);

    motify2(a);

    return 0;
}

引用做爲函數的返回值

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>


using namespace std;

char * getmem(int num)
{
    char *p = NULL;
    p = (char*)malloc(num);//分配空間

    return p;//ox11223344
}//0x1231321

int getmem2(char **pp, int num)
{
    char *p = NULL;
    p = (char*)malloc(num);

    *pp = p;

    return 0;
}//0


int getA1()
{
    int a = 10;

    return a;
}//a的值



void getA2(int *a)
{
    *a = 10;
}

//引用做爲返回值,不要返回局部變量的引用。
int& getA3()
{
    int a = 10;
    return a;
}//int &temp = a;

int &getA4()
{
    static int a = 10;

    return a;
}

int main(void)
{
    int a = 0;
    char *pp = NULL;

    a = getA1();
    pp = getmem(10);

    cout << "-----------" << endl;

    int main_a = 0;

    main_a = getA3(); //main_a = temp; //數值拷貝

    cout << "main_a " << main_a << endl;

    cout << "-----------" << endl;

#if 0   
    int &main_a_re = getA3(); 

    cout << "main_a_re " << main_a_re << endl;
    cout << "main_a_re " << main_a_re << endl;
#endif

    int &main_a_re = getA4();
    cout << "main_a_re " << main_a_re << endl;
    cout << "main_a_re " << main_a_re << endl;


    //引用若是當函數返回值的話,函數能夠當左值。
    getA4() = 1000;



    return 0;
}

指針引用

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>


using namespace std;

struct teacher
{
    int id;
    char name[64];
};


int get_mem(struct teacher** tpp)
{
    struct teacher *tp = NULL;
    tp = (struct teacher*) malloc(sizeof(struct teacher));
    if (tp == NULL) {
        return -1;
    }

    tp->id = 100;
    strcpy(tp->name, "li4");

    *tpp = tp;

    return 0;
}

void free_teacher(struct teacher **tpp)
{
    if (tpp == NULL) {
        return;
    }

    struct teacher *tp = *tpp;

    if (tp != NULL) {
        free(tp);
        *tpp = NULL;
    }
}


int get_mem2(struct teacher* &tp)
{
    tp = (struct teacher*)malloc(sizeof(struct teacher));
    if (tp == NULL) {
        return -1;
    }
    tp->id = 300;
    strcpy(tp->name, "wang5");

    return 0;
}

void free_mem2(struct teacher * &tp)
{
    if (tp != NULL) {
        free(tp);
        tp = NULL;
    }
}


int main(void)
{
    struct teacher *tp = NULL;

    get_mem(&tp);
    cout << "id =" << tp->id << ", name = " << tp->name << endl;
    free_teacher(&tp);

    cout << "00000000000" << endl;

    get_mem2(tp);
    cout << "id =" << tp->id << ", name = " << tp->name << endl;
    free_mem2(tp);

    return 0;
}
相關文章
相關標籤/搜索