#include <memory>
#include <iostream>
class DataProvider
{
public:
int x;
};
class MyClass
{
public:
const DataProvider &getDataProvider() const
{
return *data;
}
void setDataProvider(std::unique_ptr<DataProvider> newData)
{
data = std::move(newData);
}
private:
std::unique_ptr<DataProvider> data;
};
int main()
{
MyClass a;
std::unique_ptr<DataProvider> provider(new DataProvider{5});
a.setDataProvider(std::move(provider));
const DataProvider &otherProvider = a.getDataProvider();
std::cout << otherProvider.x << std::endl;
}
I2luY2x1ZGUgPG1lbW9yeT4KI2luY2x1ZGUgPGlvc3RyZWFtPgoKY2xhc3MgRGF0YVByb3ZpZGVyCnsKcHVibGljOgogICAgaW50IHg7Cn07CgpjbGFzcyBNeUNsYXNzCnsKcHVibGljOgogICAgY29uc3QgRGF0YVByb3ZpZGVyICZnZXREYXRhUHJvdmlkZXIoKSBjb25zdAogICAgewogICAgICAgIHJldHVybiAqZGF0YTsKICAgIH0KICAgIHZvaWQgc2V0RGF0YVByb3ZpZGVyKHN0ZDo6dW5pcXVlX3B0cjxEYXRhUHJvdmlkZXI+IG5ld0RhdGEpCiAgICB7CiAgICAgICAgZGF0YSA9IHN0ZDo6bW92ZShuZXdEYXRhKTsKICAgIH0KCnByaXZhdGU6CiAgICBzdGQ6OnVuaXF1ZV9wdHI8RGF0YVByb3ZpZGVyPiBkYXRhOwp9OwoKaW50IG1haW4oKQp7CiAgICBNeUNsYXNzIGE7CiAgICBzdGQ6OnVuaXF1ZV9wdHI8RGF0YVByb3ZpZGVyPiBwcm92aWRlcihuZXcgRGF0YVByb3ZpZGVyezV9KTsKICAgIGEuc2V0RGF0YVByb3ZpZGVyKHN0ZDo6bW92ZShwcm92aWRlcikpOwogICAgY29uc3QgRGF0YVByb3ZpZGVyICZvdGhlclByb3ZpZGVyID0gYS5nZXREYXRhUHJvdmlkZXIoKTsKICAgIHN0ZDo6Y291dCA8PCBvdGhlclByb3ZpZGVyLnggPDwgc3RkOjplbmRsOwp9