Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4321236
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T08:43:17+00:00 2026-05-21T08:43:17+00:00

With no experience with shared_ptr<> I am wondering whether the following is an appropriate

  • 0

With no experience with shared_ptr<> I am wondering whether the following is an appropriate use case and also whether it is a good idea to return shared_ptr<> to the user.

I have a graph like structure with multiple connections between the nodes. During traversal of the graph each node is assigned a value (computed from connected nodes) and I want the user to be able to access the value easily. The whole thing looks (strongly simplified) like this:

class Pool;
class Node {
    public:
        typedef std::tr1::shared_ptr<Node> Ptr;  
        ...
        void compute_dependencies() { 
            ...
            // calls Pool to get a new Node instance
            dependencies_.push_back(Pool::create_node(...));
            ...
        }

        // evaluate the current node
        void evaluate() { /* use dependencies_ */ };        
        double value() const { if(evaluated) return value_; };

    private:
        std::vector<Node::Ptr> dependencies_;            // vector<Node*> better?
        dbl value_;
}

// Pool creates and owns all nodes
class Pool {
    public:
        static const Node::Ptr create_node(...);         // create a new node
        void traverse_and_evaluate();      

    private:
        std::vector<Node::Ptr> allnodes;   // appropriately sorted to ensure 
                                           // dependencies are evaluated 
        ...
}

and the user calls:

Pool pool();
Node::Ptr node1 = Pool::create_node(...);
Node::Ptr node2 = Pool::create_node(...);
....
pool.traverse_and_evaluate();   
// ready to read out the now populated values
cout << node1->value() << " " << node2->value() << ... 

This has the benefit that the user gets directly access to the nodes he cares about (dependencies are often uninteresting). But I’m not 100% sure whether this is a good idea.

Thanks for your input!

Edit: There are no circular dependencies.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T08:43:17+00:00Added an answer on May 21, 2026 at 8:43 am

    shared_ptr is primarily useful for times when an object doesn’t have a clear-cut owner (or maybe needs to outlive its owner), so there’s no obvious place to destroy it. The shared_ptr essentially becomes the owner, and the object is destroyed when the last shared_ptr to it goes out of scope.

    When you’ve got a clear owner, like your Pool class, and there’s no need for the Node objects to outlive the owning Pool, then there really isn’t much need for a shared_ptr. You can just destroy the objects in the owner’s destructor.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Do you have any experience of designing a Real Shared-Nothing Architecture? Would you have
Anyone have experience with adding a ReSharper profile to the VS2008 solution and share
What experience have you had with introducing a Ribbon style control to legacy MFC
Does anyone have any experience getting MSTest to copy hibernate.cfg.xml properly to the output
Does anyone have experience with or know of a web host that allows the
I have no experience with low level programing and I need this piece of
Although having a little experience, I am still confused with the question of whether
In my experience I have only had 2 patterns work for large-scale desktop application
I have a Silverlight 4 application where I am making use of shared classes.
I know how to approach this problem but i have very little experience dealing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.