Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

C++ class that may or may not own resource for convienence

39 views
Skip to first unread message

joe...@gmail.com

unread,
Mar 21, 2017, 8:17:10 AM3/21/17
to
Is there a more well worn idiom for a class that, by default, will own its own resource, but will also be happy to have that resource externally created/managed?

For example:

#include <memory>

class Resource
{
//stuff
};

class Report
{
public:
Report() { report = std::make_shared<Resource>();}
Report(Resource* a) { report= std::shared_ptr<Resource>(a, [](Resource*){});}

private:
std::shared_ptr<Resource> report;

};

Class above can use default copy constructor or default assignment operator and the shared ptr should always carry the right deleter here I believe.

Can anyone offer improvements here? Is there a more well worn pattern here?

JC

Öö Tiib

unread,
Mar 22, 2017, 9:23:42 AM3/22/17
to
On Tuesday, 21 March 2017 14:17:10 UTC+2, joe...@gmail.com wrote:
> Is there a more well worn idiom for a class that, by default, will own its own
> resource, but will also be happy to have that resource externally created/managed?

Motivating example of problem is missing for me for such
"maybe managing".

Alf P. Steinbach

unread,
Mar 22, 2017, 3:44:55 PM3/22/17
to
String.

Cheers!,

- Alf


0 new messages