指針的引用和返回指針的引用

對於返回指針的引用,如果想用一個指針來引用這個function返回的引用,必須在這種情況下:int * &p = haha(q);(其中 int * &haha(int *&g))
如果是int * p = haha(q); 那麼就相當於haha爲int * haha(int * &g)

注意:
下面的定義是錯誤的:
int *&p;
屬於語法錯誤;

這裏寫圖片描述

這裏寫圖片描述

這裏寫圖片描述

#include<iostream>
#include<cmath>
using namespace std;

int z = 2;

int *&haha(int *&g)
{
    g = &z;
    cout << "g posi: " << g << endl;
    return g;
}

int main()
{

    int x = 0;
    int y = 1;

    //int *p;
    int *q = &y;

    int *&p = haha(q);

    cout << *q;
    cout << *p;

    cout << "q posi: " << q << endl;
    cout << "p posi: " << p << endl;
    p = &x;
    cout << *q;
    cout << *p;


    // << *p;
    return 0;
}

g posi: 01379004
22q posi: 01379004
p posi: 01379004
00請按任意鍵繼續.

#include<iostream>
#include<cmath>
using namespace std;

int z = 2;

int *haha(int *&g)
{
    g = &z;
    cout << "g posi: " << g << endl;
    return g;
}

int main()
{

    int x = 0;
    int y = 1;

    //int *p;
    int *q = &y;

    int *p = haha(q);

    cout << *q;
    cout << *p;

    cout << "q posi: " << q << endl;
    cout << "p posi: " << p << endl;
    p = &x;
    cout << *q;
    cout << *p;


    // << *p;
    return 0;
}

g posi: 009F9004
22q posi: 009F9004
p posi: 009F9004
20請按任意鍵繼續…

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