*和*&的区别是什么?
#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函数中的*&全换成*就不对了?谁能跟我解释下*&是什么意思?*指针不是已经通过传地址改变值了么?