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
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message