I'm developing c++ project in c++11 (gcc 4.8.2). Recently I found unique_ptr
is useful for me. Unfortunately I can't use the std::make_unique
function in my environment. So I tried lazy initialization of unique_ptr
by using std::move
.
Actually, the below code works, I don't have confidence in myself. Could you give any comments on better ways to initialize a unique_ptr
? I think my initialization is a little redundant.
class AppData {
public:
AppData(int id):_id(id){};
int _id;
void print() { std::cout << "id is " << _id << std::endl; };
};
class Test {
public:
Test(){};
~Test(){};
void test();
std::unique_ptr<AppData> p_data;
};
void Test::test() {
// I am concerned with this part
std::unique_ptr<AppData> p(new AppData(3));
p_data = std::move(p);
p_data->print();
}
int main() {
Test t;
t.test();
return 0;
}