注册 登录
编程论坛 C++教室

*和*&的区别是什么?

xph4444 发布于 2008-10-31 00:29, 834 次点击
#include <iostream>
using namespace std;
class Goods
{
private:
int weight;
static int total;
Goods *next;
public:
Goods(int w);
friend void purchase(Goods *&f,Goods *&r);
friend void sale(Goods *&f);
};
int Goods::total=0;
Goods::Goods(int w)
{
weight=w,total+=weight;
}

void purchase(Goods *&f,Goods *&r)
{
cout<<"买入:";
int w;
cin>>w;
Goods *b=new Goods(w);
b->next=NULL;
if(f==NULL)
f=r=b;
else
{
r->next=b,r=r->next;
}
cout<<"剩余货物:"<<b->total<<endl;
}
void sale(Goods *&f)
{

if(f==NULL)
cout<<"没有库存!"<<endl;
else
{
Goods *p;
cout<<"卖出:"<<f->weight<<endl;
Goods::total-=f->weight;
p=f,f=f->next,delete p;
cout<<"剩余货物:"<<f->total<<endl;
}
}
main()
{
Goods *f=NULL,*r int i;
do
{
cout<<"1 为买入,2 为卖出,0 为结束,请输入:"<<endl;
cin>>i;
switch(i)
{
case 1:
{
purchase(f,r);
break;
}
case 2:
{
sale(f);
break;
}
case 0:
break;
}
}while(i);
return 0;
}
上面的程序是正确的,但为什么把purchase和sale函数中的*&全换成*就不对了?谁能跟我解释下*&是什么意思?*指针不是已经通过传地址改变值了么?
4 回复
#2
随心2008-10-31 00:49
应该是引用的指针吧
#3
lionmusicyj2008-10-31 01:01
看上去像是双重指针哟~!
呵呵~!小弟的菜鸟,来论坛学习的,有说的不对,不要见怪哈~!
#4
lockhawk2008-10-31 01:56
为了明天看回复,先做个记号
#5
danielxu2008-10-31 08:26
错误!止步

[[it] 本帖最后由 danielxu 于 2008-11-17 11:48 编辑 [/it]]
1