Here is a bug that compiles cleanly and fails silently: a loop marks every task as done, the program runs, and every task is still marked "Todo." The loop did change something. It changed copies, and each copy was thrown away at the end of its iteration. To write updates that stick, you need to know exactly when C++ copies a record and when it refers to the original.
Copying a Task copies its string and other fields. The copy owns its own std::string, with its own characters, so changing the copy never alters the original, and changing the original never alters the copy. A Task& reference is different: it is an alias, another name for an existing task, and writing through it changes that task.
Predict and run
#include <iostream>
#include <string>
struct Task {
std::string title;
bool done = false;
};
int main() {
Task original{"Read", false};
Task copy = original;
copy.done = true;
std::cout << original.done << " " << copy.done << "\n";
Task& same = original;
same.done = true;
std::cout << original.done << "\n";
return 0;
}Check your prediction
0 1
1copy started as an exact duplicate, then became a separate task with its own flag. same is not a task at all; it is a second name for original, so the change through same is a change to original. Change copy.title to "Copy" and print both titles: original still says Read, because each object owns its own string.
Three ways to loop over records
The same distinction applies to the loop variable in a range-based for loop. Choose the form that matches what the loop is for:
for (Task task : tasks)copies each element. Changes are lost; reading also pays for copying every title.for (Task& task : tasks)refers to each element. Use it when the loop updates the tasks.for (const Task& task : tasks)refers to each element without permission to change it. Use it when the loop only reads.
#include <iostream>
#include <string>
#include <vector>
struct Task {
std::string title;
bool done = false;
};
int main() {
std::vector<Task> tasks{{"Read", false}, {"Review", false}};
for (Task task : tasks) { // copies: the update is lost
task.done = true;
}
std::cout << tasks[0].done << " " << tasks[1].done << "\n";
for (Task& task : tasks) { // references: the update sticks
task.done = true;
}
std::cout << tasks[0].done << " " << tasks[1].done << "\n";
return 0;
}0 0
1 1The two loop bodies are identical. Only the & decides whether the vector changes. If you write a loop intending to read and accidentally change a field, const Task& turns that mistake into a compile error, which is why it is the right default for reading.
The same rule for single elements and parameters
A copy hides in ordinary declarations too. Task first = tasks[0]; makes a new task; changing first.done leaves tasks[0] alone. Task& first = tasks[0]; names the element itself. Function parameters follow the same rule:
#include <iostream>
#include <string>
struct Task {
std::string title;
bool done = false;
};
void mark_copy(Task task) {
task.done = true;
}
void mark(Task& task) {
task.done = true;
}
int main() {
Task task{"Practice", false};
mark_copy(task);
std::cout << task.done << "\n";
mark(task);
std::cout << task.done << "\n";
return 0;
}0
1Copies are not bad. They are the right choice when you want an independent value, for example a backup before an experiment, or a record stored in a vector that must not change if the caller's variable changes later. The planner's add_task relies on exactly that: it stores a copy, so changing the caller's Task afterward does not alter the stored one. Copy when you want independence; refer when you want to affect the original.
Practice
Your turn
Write int complete_long(std::vector<Task>& tasks, int limit) that marks every task with at least limit minutes as done, and returns how many tasks it changed from not done to done. Tasks that were already done must not be counted. With these tasks and a limit of 25:
Read, 25 minutes, not doneReview, 10 minutes, not donePractice, 40 minutes, already done
print each task's title and flag, then the count:
Read 1Review 0Practice 1Changed: 1
Calling the function a second time should return 0.
#include <iostream>
#include <string>
#include <vector>
struct Task {
std::string title;
int minutes = 0;
bool done = false;
};
int main() {
std::vector<Task> tasks{{"Read", 25, false}, {"Review", 10, false}, {"Practice", 40, true}};
// Write complete_long above main, call it, then print.
return 0;
}Hint 1: the idea
The function must change the caller's tasks, so both the parameter and the loop variable need &. Count a change only when a task qualifies and is not already done.
Hint 2: a first step
First write a version that only marks tasks, with int changed = 0; and return changed; around a loop over Task& task. Print the flags: you should see Read 1, Review 0, Practice 1. Then decide where to count, and check that a second call returns 0.
Show one solution
#include <iostream>
#include <string>
#include <vector>
struct Task {
std::string title;
int minutes = 0;
bool done = false;
};
int complete_long(std::vector<Task>& tasks, int limit) {
int changed = 0;
for (Task& task : tasks) {
if (task.minutes >= limit && !task.done) {
task.done = true;
++changed;
}
}
return changed;
}
int main() {
std::vector<Task> tasks{{"Read", 25, false}, {"Review", 10, false}, {"Practice", 40, true}};
int changed = complete_long(tasks, 25);
for (const Task& task : tasks) {
std::cout << task.title << " " << task.done << "\n";
}
std::cout << "Changed: " << changed << "\n";
return 0;
}Read 1
Review 0
Practice 1
Changed: 1The update loop uses Task& because it changes records, and the printing loop uses const Task& because it only reads them. Checking !task.done before counting makes the function safe to repeat: a second call finds nothing new and returns 0. Next you will look at how long objects live, and why a reference must never outlive the object it names.
Completion is saved in this browser only. It does not measure mastery.