Please note that with the words "object" and "move" in the title, I do not mean the C++-specific concepts of what an object is and what it means to move an object.
I have a local variable of a pretty simple struct type where I accumulate some values read from a config file.
struct Foo
{
std::string name;
float someValue;
int someStuff;
};
// This stores the current settings
std::shared_ptr<Foo> currentFooSetting;
void readSettings()
{
Foo f;
f.name = ...;
f.someValue = ...;
f.someStuff = ...;
if (everythingIsOk)
{
//TODO:
//Delete the old object pointed to by currentFooSetting (if it's not pointed to by any other shared_ptr, of course)
//Allocate some new memory to put f into... or maybe reuse *currentFooSetting if it's now free idc.
//Copy f to the new memory location
//
}
}
If everything is OK, I want to move f
onto the heap and have currentFooSetting point to that.
How do I do that?
It seems I could do this:
std::shared_ptr<Foo> newFooSetting = std::make_shared<Foo>();
*newFooSetting = f;
currentFooSetting = newFooSetting;
(Using a local variable to better emphasize the separation between the allocation, copying, and replacing.) But even after reading through https://en.cppreference.com/w/cpp/memory/shared_ptr and https://en.cppreference.com/w/cpp/memory/shared_ptr/make_shared I have no idea whether that's "the way" of doing this.