Last active
October 21, 2015 03:40
-
-
Save xunkai55/864541ba47a3f92725a9 to your computer and use it in GitHub Desktop.
unique_ptr usage
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 <vector> | |
#include <memory> | |
using namespace std; | |
class Base { | |
public: | |
Base(int x): flag(x) {} | |
int flag; | |
void say() { cout << "I'm " << flag << endl; } | |
}; | |
typedef unique_ptr<Base> Unique; | |
typedef vector<Unique> UniqueList; | |
void LSay(UniqueList v) { | |
//for (auto g: v) { | |
// g->say(); | |
//} | |
} | |
void LSay2(UniqueList& v) { | |
//for (auto g: v) { | |
// g->say(); | |
//} | |
} | |
void LSay3(UniqueList& v) { | |
for (auto &g: v) { | |
g->say(); | |
} | |
} | |
void USay(Unique u) { | |
u->say(); | |
u->flag = 1; | |
u->say(); | |
} | |
void BSay(Base &b) { | |
b.say(); | |
b.flag = 2; | |
b.say(); | |
} | |
int main() { | |
Unique u(new Base(5)); | |
//USay(u); | |
BSay(*u); | |
u->say(); | |
UniqueList v; | |
v.push_back(std::move(u)); | |
//LSay(v); | |
//LSay2(v); | |
LSay3(v); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment