Created
September 30, 2014 13:42
-
-
Save mitsu-ksgr/c0468aa21167f86158ff to your computer and use it in GitHub Desktop.
【C++】std::unique_ptrをvectorで扱うメモ
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 <vector> | |
struct Test { | |
Test(int data = 0) : data(data) { | |
std::cout << "Test::Constructor: " << this->data << std::endl; | |
} | |
~Test() { | |
std::cout << "Test::Destructor: " << this->data << std::endl; | |
} | |
int data; | |
}; | |
int main() | |
{ | |
std::vector<std::unique_ptr<Test>> vec; | |
for(int i = 0; i < 5; ++i) { | |
vec.push_back( | |
std::unique_ptr<Test>(new Test(i))); | |
} | |
for(auto &elem : vec) { | |
std::cout << elem->data << std::endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment