I have the following code:
class Thing
{};
void fnc(Thing** out)
{
*out = new Thing();
};
Where fnc
returns a new instance of Thing
via output parameter. Normally I'd use it as follows:
int main()
{
Thing* thing;
fnc(&thing);
}
Can I put the returned object in a std::unique_ptr
instead?
int main()
{
std::unique_ptr<Thing> uniqueThing;
fnc(???);
}
To extend your code sample, it (i.e. pass-by-pointer) would be
void fnc(std::unique_ptr<Thing>* out)
{
out->reset(new Thing());
};
int main()
{
std::unique_ptr<Thing> uniqueThing;
fnc(&uniqueThing);
}
or use pass-by-reference:
void fnc(std::unique_ptr<Thing>& out)
{
out.reset(new Thing());
};
int main()
{
std::unique_ptr<Thing> uniqueThing;
fnc(uniqueThing);
}
But I think return the pointer by return value would be more clear:
std::unique_ptr<Thing> fnc()
{
return std::unique_ptr<Thing>(new Thing);
// or
// return std::make_unique<Thing>();
};
int main()
{
auto uniqueThing = fnc();
}
You should store the raw pointer in the unique_ptr
so that the unique_ptr
takes ownership:
Thing* thing;
fnc(&thing);
std::unique_ptr<Thing> thing_unique (thing);
You can't do that directly. You'll have to use a temporary variable:
Thing* thing{};
fnc(&thing);
uniqueThing.reset(*thing);
Or even better, because fnc
doesn't a return an error code or something else, you can just return the new pointer which let's write initialize uniqueThing
directly:
std::unique_ptr<Thing> uniqueThing{fnc()};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With