A struct can be a function parameter just like an integer or a vector. The choice between a value, a const reference, and a writable reference still tells the reader whether the function gets a copy, reads an existing object, or may change it.
#include <string>
#include <iostream>
struct Session {
std::string title;
int seats;
};
bool hasSpace(const Session& session) {
return session.seats > 0;
}
void addSeat(Session& session) {
++session.seats;
}
int main() {
Session art{"Art", 0};
addSeat(art);
std::cout << hasSpace(art) << '\n';
}
This prints 1, the ordinary stream representation of true. hasSpace can read the fields but cannot change the record through its const Session& parameter. addSeat uses Session& without const, so its increment reaches the caller's art record. At each call site, pass art normally.
If the parameter were simply Session session, the function would receive a copy. Its field changes would not update the caller. That can be useful for independent calculations, but it would be the wrong choice for an operation named addSeat whose purpose is to mutate the supplied record.
Your stock helper pair separates these roles. hasStock reports whether a record's unit count is positive. addUnits increases a record by a supplied nonnegative delivery. The fixtures use small nonnegative counts, so the additions stay within the integer range.
The first output line calls hasStock with pens. The second reports pads after a call to addUnits. Because these are different records, you can check the reading function before writing the update. Keep the supplied parameter declarations: the presence or absence of const already gives each function the access it needs.
item.units is positive from hasStock. Expect Available: 1 for the supplied record.addUnits, add delivered to the referenced record's units field. Expect Updated units: 7.Edit main.cpp, then select Run to inspect the Console. Select Submit to check your completed tasks.