【发布时间】:2015-12-24 13:31:25
【问题描述】:
我有这个结构
typedef struct _flight {
FlightId flightId;
int totalPlaces;
int booked;
float currentPrice;
float initialPrice;
void Print(int index);
void RecalculatePrice();
bool BookSeats(int amount);
} Flight;
这会造成麻烦,BookSeats:
bool Flight::BookSeats(int cantidad) {
if (booked + cantidad <= totalPlaces) {
booked = booked + cantidad;
RecalculatePrice();
return true;
}
return false;
}
当我调用 BookSeats 时,值的变化不会存储在方法的末尾(比如当你将一个变量按值传递给参数时),我尝试使用
this->booked = booked + cantidad; // but doesn't work too;
我做错了什么?我不知道是否是参考问题,但是...变量和 void 都在结构内。我不明白。
【问题讨论】:
-
确定满足条件?
-
如果能给出一个完整的例子就更好了?
-
Flight的构造函数是什么样的? -
为什么我们需要 MCVE 的教科书示例。如果你还没有写过,那么你还没有完成调试。
-
如果你“没有
sizeof”那么就大错特错了,你实际上并不是在写C++,这一切都毫无意义。
标签: c++ function struct pass-by-reference