對於返回指針的引用,如果想用一個指針來引用這個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請按任意鍵繼續…