Skip to content

Instantly share code, notes, and snippets.

@bruceoutdoors
Created September 4, 2014 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bruceoutdoors/fe2cce86d10479f447c2 to your computer and use it in GitHub Desktop.
Save bruceoutdoors/fe2cce86d10479f447c2 to your computer and use it in GitHub Desktop.
Used for tutorial: http://wp.me/p2N4HD-1L
#include <iostream>
#include <memory>
#include <string>
#include <cassert>
#include "Point.hpp"
class PointMan
{
public:
static const size_t SIZE = 3;
PointMan() {
for (size_t i = 0; i < SIZE; i++) {
std::stringstream ss;
ss << i;
Point::UPtr temp(new Point("element-" + ss.str()));
temp->set(i, i*2);
container[i] = std::move(temp);
assert(temp == nullptr);
/*
// alternatively...
container[i] = Point::UPtr(new Point("element-" + ss.str()));
container[i]->set(i, i*2);
*/
}
mainPoint = container[1].get();
// delete mainPoint; // don't do this! no no no!
}
void print() {
std::cout << "Unique Pointer Container Points: \n";
for (size_t i = 0; i < SIZE; i++) {
std::cout << " > Point " << i
<< container[i]->toString() << "\n";
}
std::cout << "\nmainPoint: "
<< mainPoint->toString() << "\n\n";
}
private:
Point *mainPoint;
std::array<Point::UPtr, SIZE> container;
};
int main()
{
PointMan man;
man.print();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment