对于返回指针的引用,如果想用一个指针来引用这个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请按任意键继续…