c++11 - How to properly use shared_ptr in good C++ APIs -



c++11 - How to properly use shared_ptr in good C++ APIs -

i'm trying find out how utilize shared_ptr feature of c++11 in c++ apis. main area need in container classes (like nodes in scene graph illustration may contain list of kid nodes , reference parent node , stuff that). creating copies of nodes not alternative , using references or pointers pain in ass because no 1 knows responsible destructing nodes (and when destructs node still referenced other node programme crash).

so think using shared_ptr may thought here. let's take @ next simplified illustration (which demonstrates kid node must connected parent node):

#include <memory> #include <iostream> using namespace std; class parent {}; class kid { private: shared_ptr<parent> parent; public: child(const shared_ptr<parent>& parent) : parent(parent) {} parent& getparent() { homecoming *parent.get(); } }; int main() { // create parent shared_ptr<parent> parent(new parent()); // create kid parent kid child(parent); // other code may need parent kid 1 time again this: parent& p = child.getparent(); ... homecoming 0; }

this api forces user utilize shared_ptr creating actual connection between kid , parent. in other methods want more simple api, that's why getparent() method returns reference parent , not shared_ptr.

my first question is: right usage of shared_ptr? or there room improvement?

my sec question is: how react on null-pointers? because getparent method returns reference user may think never can homecoming null. that's wrong because homecoming null when passes shared pointer containing null-pointer constructor. don't want null pointers. parent must set. how handle this? manually checking shared pointer in constructor , throwing exception when contains null? or there improve way? maybe sort of non-nullable-shared-pointer?

using shared pointers purpose describe reasonable , increasingly mutual in c++11 libraries.

a few points note:

on api, taking shared_ptr argument forces caller build shared_ptr. move there transfer of ownership of pointee. in cases function simply uses shared_ptr, may acceptable take reference object or shared_ptr

you using shared_ptr<parent> hold reference parent object whilst using 1 in other direction. create retain-cycle resulting in objects never deleted. in general, used shared_ptr when referencing top down, , weak_ptr when referencing up. watch out in particular delegate/callback/observer objects - these want weak_ptr callee. need take care around lambdas if executing asynchronously. mutual pattern capture weak_ptr.

passing shared pointers reference rather value stylistic point arguments , against. when passing reference not passing ownership (e.g. increasing reference count on object). on other hand, not taking overhead either. there danger under reference objects way. on more practical level, c++11 compiler , standard library, passing value should result in move rather re-create construction , free anyway. however, passing reference makes debugging considerably easier won't repeatedly stepping shared_ptr's constructor.

construct shared_ptr std::make_shared rather new() , shared_ptr's constructor

shared_ptr<parent> parent = std::make_shared<parent>();

with modern compilers , libraries can save phone call new().

both shared_ptr , weak_ptr can contain null - other pointer can. should in habit of checking before dereferencing , assert()ing liberally too. constructor case, can take null pointers , instead throw @ point of use.

you might consider using typedef shared pointer type. 1 style used follows:

typedef std::weak_ptr<parent> parent_p;

typedef std::shared_ptr<parent> parent_wkp;

typedef std::weak_ptr<child> child_p;

typedef std::shared_ptr<child> child_wkp;

it's useful know in header files can forwards declare shared_ptr<type> without having seen total declaration type. can save lot of header bloat

c++ c++11

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -