-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrefWrap.cpp
48 lines (45 loc) · 1.17 KB
/
CrefWrap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// cont/refsem1.cpp
#include <iostream>
#include <string>
#include <set>
#include <deque>
#include <algorithm>
#include <memory>
#include <vector>
class Item {
private:
std::string name;
float price;
public:
Item (const std::string& n, float p = 0) : name(n), price(p) {
}
std::string getName () const {
return name;
}
void setName (const std::string& n) {
name = n;
}
float getPrice () const {
return price;
}
float setPrice (float p) {
price = p;
}
};
main() {
//std::vector<Item&> book1s; // wouldn't compile
std::vector<std::reference_wrapper<Item>> books; // elements are references
Item f("Faust",12.99);
books.push_back(f); // insert book by reference
// print books:
for (const auto& book : books) {
std::cout << book.get().getName() << ": "
<< book.get().getPrice() << std::endl; //12.99
}
f.setPrice(9.99); // modify book outside the containers
std::cout << books[0].get().getPrice() << std::endl; // print price of first book 9.99
// print books using type of the elements (no get() necessary):
for (const Item& book : books) {
std::cout << book.getName() << ": " << book.getPrice() << std::endl; // 9.99
}
}