关于C++中:*&与*的问题
初学C++类,求指教:代码如下:这是一道计数进出货质量的问题,每个节点设为GOODS,
每次保存的货物质量为weight,设置静态变量total_weight计数总重。
#include<iostream>
using namespace std;
class GOODS {
public:
GOODS(int w) {
weight = w;
total_weight += w;
}
~GOODS(){
total_weight -= weight;
}
int Weight() {
return weight;
}
static int Total_weight() {
return total_weight;
}
GOODS *next;
private:
int weight;
static int total_weight;
};
int GOODS::total_weight = 0;
void purchase(GOODS *&f, GOODS *&r, int w) {
GOODS *p = new GOODS(w);
p->next = NULL;
if (f == NULL) {
f = r = p;
}
else {
r->next = p;
r = r->next;
}
}
void sale(GOODS *&f, GOODS *&r) {
if (f == NULL) {
cout << "No any GOODS!" << endl;
return;
}
GOODS *p = f;
f = f->next;
delete p;
p = NULL;
cout << "saled" << endl;
}
int main() {
GOODS *f = NULL;
GOODS *r = NULL;
int w,choice;
do {
cin >> choice;
switch (choice) {
case 1: {cin >> w; purchase(f, r, w); break; }//1表示进货,w为此次进货的重量
case 2: {sale(f, r); break; } //2表示出货,从表头开始出货
case 0:{break;} //0表示退出
}
cout << "Now total weight is " << GOODS::Total_weight() << endl;
} while (choice);
return 0;
}
这个时候输出时正常的,
但当把
void purchase(GOODS *f, GOODS *r, int w)
void sale(GOODS *f, GOODS *r)
这两个函数的&去掉后输出确实不正常的,
请问*&和*有什么区别?
[此贴子已经被作者于2016-3-20 14:28编辑过]