Passing shared_ptr by reference - is it a good idea [duplicate]

2 days ago 4
ARTICLE AD BOX

When working with C++ normally you pass the variable by reference if you want to get the modified data from the function.

However, in case of std::shared_ptr<> Google says that this is a bad idea and often indicates a design flaw. Could someone please look at what I'm doing and see if there is a better way.

Just trying to improve the code by using the smart pointers...

struct BaseData { std::string name; }; struct DerivedData1 : public BaseData { bool check1; }; struct DerivedData2 : public BaseData { int param; } class Foo { void Bar(bool param, std::shared_ptr<BaseData> options); }; void MyFunc1() { std::shared_ptr<BaseData> options; Foo foo; foo.Bar(true, options); } void Foo::Bar(bool param, std::shared_ptr<BaseData> options) { if( param ) options = make_shared<DerivedData1>(); else options = make_shared<DerivedData2>(); }

Now I do realize that every situation is different, but if there is a design flaw here - I'd like to avoid it to improve the code and not make it worse. ;-)

Some clarification:

Class Foo is handling the DB processing. So I'm creating some options for different DBMSes, then pass them to the UI and then modified options goes back to Foo in order to apply as appropriate.

Read Entire Article