If I have two functions like:
1.
vector<Student_Info> extract_failed(vector<Student_Info>& students) {
return students;
}
2.
vector<Student_Info>& extract_failed(vector<Student_Info>& students) {
return students;
}
Is it correct to say that function 1 is returning by value
, which means a copy of students
will be created and returned and function 2 is returning by reference
?
Is it correct to say that function 1 is returning by value, which means a copy of students will be created and returned and function 2 is returning by reference?
Yes, it is correct. Another way to phrase it, which I prefer, is to say that 1 returns a value, and 2 returns a reference.
YES
Although 2. in its current form would make sence if you plan to use the return value (something like print(extract_failed(students));
or for(const auto& s : extract_failed(students)
).
Meanwhile, some scenarios may be confusing, since you can do something like
auto& new_students = extract_failed(students);
and you'll have two names in the same scope for the same vector.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.