I have a main function that takes in a reference of a class object and try to update it via foo() and bar(), however, the bar() only allows to pass into a shared_ptr type. How can I guarantee it behaves as expected, assuming I am NOT allowed to modify the signature of bar().
void foo(MyStruct& s) { modifyS(s); } void bar(std::shared_ptr<MyStruct> s) { modifySAgain(s); } mainFunction(MyStruct& s) { foo(s); // bar(?) how should I do here such that s would be modified by bar() // bar(std::make_shared<Mystruct>(std::move(s)) ? }
barthen why not just make itvoid bar(MyStruct& s)just likefoo?baractually requires astd::shared_ptrto work correctly, or it only requires aMyStruct&to work correctly. If the latter,baris written incorrectly and should be fixed.enabled_shared_from_this. That said, I think the design is the issue here. If bar requires ownership, andmainFunctioncalls bar, then transitivelymainFunctionrequires ownership too. But free functions should relatively rarely need to take ownership anyhow.