Created
September 4, 2014 16:12
-
-
Save bruceoutdoors/fe2cce86d10479f447c2 to your computer and use it in GitHub Desktop.
Used for tutorial: http://wp.me/p2N4HD-1L
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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