Ownership
Vector Ownership
A container can own several objects through smart pointers.
Vector Ownership
vector_ownership.cpp
#include <iostream>
#include <memory>
#include <vector>
class Box {
private:
int value;
public:
Box(int start) {
value = start;
}
int getValue() {
return value;
}
};
int main() {
int first = ;
int second = ;
std::vector<std::unique_ptr<Box>> boxes;
boxes.push_back(std::make_unique<Box>(first));
boxes.push_back(std::make_unique<Box>(second));
int total = 0;
for (const std::unique_ptr<Box>& box : boxes) {
total += box->getValue();
}
std::cout << "count=" << boxes.size() << std::endl;
std::cout << "total=" << total << std::endl;
return 0;
}
#include <iostream>
#include <memory>
#include <vector>
class Box {
private:
int value;
public:
Box(int start) {
value = start;
}
int getValue() {
return value;
}
};
int main() {
int first = ;
int second = ;
std::vector<std::unique_ptr<Box>> boxes;
boxes.push_back(std::make_unique<Box>(first));
boxes.push_back(std::make_unique<Box>(second));
int total = 0;
for (const std::unique_ptr<Box>& box : boxes) {
total += box->getValue();
}
std::cout << "count=" << boxes.size() << std::endl;
std::cout << "total=" << total << std::endl;
return 0;
}
#include <iostream>
#include <memory>
#include <vector>
class Box {
private:
int value;
public:
Box(int start) {
value = start;
}
int getValue() {
return value;
}
};
int main() {
int first = ;
int second = ;
std::vector<std::unique_ptr<Box>> boxes;
boxes.push_back(std::make_unique<Box>(first));
boxes.push_back(std::make_unique<Box>(second));
int total = 0;
for (const std::unique_ptr<Box>& box : boxes) {
total += box->getValue();
}
std::cout << "count=" << boxes.size() << std::endl;
std::cout << "total=" << total << std::endl;
return 0;
}
#include <iostream>
#include <memory>
#include <vector>
class Box {
private:
int value;
public:
Box(int start) {
value = start;
}
int getValue() {
return value;
}
};
int main() {
int first = ;
int second = ;
std::vector<std::unique_ptr<Box>> boxes;
boxes.push_back(std::make_unique<Box>(first));
boxes.push_back(std::make_unique<Box>(second));
int total = 0;
for (const std::unique_ptr<Box>& box : boxes) {
total += box->getValue();
}
std::cout << "count=" << boxes.size() << std::endl;
std::cout << "total=" << total << std::endl;
return 0;
}
#include <iostream>
#include <memory>
#include <vector>
class Box {
private:
int value;
public:
Box(int start) {
value = start;
}
int getValue() {
return value;
}
};
int main() {
int first = ;
int second = ;
std::vector<std::unique_ptr<Box>> boxes;
boxes.push_back(std::make_unique<Box>(first));
boxes.push_back(std::make_unique<Box>(second));
int total = 0;
for (const std::unique_ptr<Box>& box : boxes) {
total += box->getValue();
}
std::cout << "count=" << boxes.size() << std::endl;
std::cout << "total=" << total << std::endl;
return 0;
}
#include <iostream>
#include <memory>
#include <vector>
class Box {
private:
int value;
public:
Box(int start) {
value = start;
}
int getValue() {
return value;
}
};
int main() {
int first = ;
int second = ;
std::vector<std::unique_ptr<Box>> boxes;
boxes.push_back(std::make_unique<Box>(first));
boxes.push_back(std::make_unique<Box>(second));
int total = 0;
for (const std::unique_ptr<Box>& box : boxes) {
total += box->getValue();
}
std::cout << "count=" << boxes.size() << std::endl;
std::cout << "total=" << total << std::endl;
return 0;
}
#include <iostream>
#include <memory>
#include <vector>
class Box {
private:
int value;
public:
Box(int start) {
value = start;
}
int getValue() {
return value;
}
};
int main() {
int first = ;
int second = ;
std::vector<std::unique_ptr<Box>> boxes;
boxes.push_back(std::make_unique<Box>(first));
boxes.push_back(std::make_unique<Box>(second));
int total = 0;
for (const std::unique_ptr<Box>& box : boxes) {
total += box->getValue();
}
std::cout << "count=" << boxes.size() << std::endl;
std::cout << "total=" << total << std::endl;
return 0;
}
#include <iostream>
#include <memory>
#include <vector>
class Box {
private:
int value;
public:
Box(int start) {
value = start;
}
int getValue() {
return value;
}
};
int main() {
int first = ;
int second = ;
std::vector<std::unique_ptr<Box>> boxes;
boxes.push_back(std::make_unique<Box>(first));
boxes.push_back(std::make_unique<Box>(second));
int total = 0;
for (const std::unique_ptr<Box>& box : boxes) {
total += box->getValue();
}
std::cout << "count=" << boxes.size() << std::endl;
std::cout << "total=" << total << std::endl;
return 0;
}
#include <iostream>
#include <memory>
#include <vector>
class Box {
private:
int value;
public:
Box(int start) {
value = start;
}
int getValue() {
return value;
}
};
int main() {
int first = ;
int second = ;
std::vector<std::unique_ptr<Box>> boxes;
boxes.push_back(std::make_unique<Box>(first));
boxes.push_back(std::make_unique<Box>(second));
int total = 0;
for (const std::unique_ptr<Box>& box : boxes) {
total += box->getValue();
}
std::cout << "count=" << boxes.size() << std::endl;
std::cout << "total=" << total << std::endl;
return 0;
}
owning container
A `std::vector<std::unique_ptr<T>>` owns each object stored in the vector.