#include <iostream>
#include <vector>
#include <memory>
class GetVariableDataType
{
public:
int x;
};
class GetVariablesRequest
{
public:
// No need to customize the destructor, default one is ok
//~GetVariablesRequest() { }
std::vector<std::unique_ptr<GetVariableDataType>> getVariableData;
};
int
main ()
{
{
// request is a smart pointer
std::unique_ptr<GetVariablesRequest> request =
std::make_unique<GetVariablesRequest>();
auto type1 = std::make_unique<GetVariableDataType>();
auto type2 = std::make_unique<GetVariableDataType>();
request->getVariableData.push_back(std::move(type1));
request->getVariableData.push_back(std::move(type2));
request->getVariableData[0]->x = 1;
request->getVariableData[1]->x = 2;
// request is deleted when it goes out of scope (next line)
}
{
// request is a raw pointer
GetVariablesRequest *request = new GetVariablesRequest ();
auto type1 = std::make_unique<GetVariableDataType>();
auto type2 = std::make_unique<GetVariableDataType>();
request->getVariableData.push_back(std::move(type1));
request->getVariableData.push_back(std::move(type2));
// request must be deleted to avoid leaks
delete request;
}
{
// request is on the stack
GetVariablesRequest request;
auto type1 = std::make_unique<GetVariableDataType>();
auto type2 = std::make_unique<GetVariableDataType>();
request.getVariableData.push_back(std::move(type1));
request.getVariableData.push_back(std::move(type2));
// request is deleted when it goes out of scope (next line)
}
return 0;
}