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 9094861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:27:16+00:00 2026-06-16T23:27:16+00:00

I have a directed acyclic graph, composed of Node objects. Each node has a

  • 0

I have a directed acyclic graph, composed of Node objects. Each node has a list of std::shared_ptrs to other nodes, which are its children in the graph. I have lots of useful methods I need, such as inserting/emplacing/reparenting nodes, testing if a node is an ancestor of another, etc. Some are standard STL-like methods, and some are specific to directed acyclic graphs and specific to my needs.

The question is, When such a method takes a node as a parameter, should it take a reference? of a weak_ptr? or a shared_ptr? I tried to examine use cases but it’s hard to tell. What’s the best design here? I’m new to smart pointers and I’m not sure what’s the best choice. Should I treat shared_ptr<Node> as “the representation” of node objects? Or maybe the way to choose is more sophisticated?

Thanks in advance

  • 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-06-16T23:27:17+00:00Added an answer on June 16, 2026 at 11:27 pm

    Only pass a shared_ptr (by value) or copy it when the set of owners is meaningfully extended. It’s safe, and preferred, to pass pointers when dealing with nodes as pure information.

    Note the std::enable_shared_from_this facility to retrieve the correct std::shared_ptr from any graph object. With that base class, a valid naked pointer and a shared pointer are essentially equivalent. I’m not sure how much, if any, overhead it adds. (It definitely ensures that there will be no additional heap fragmentation, which std::make_shared also does.)

    Passing shared_ptr anywhere is just an optimization of functionality elegantly provided by shared_from_this. But when you do, pass them by const reference, since they are just providing information without actively arbitrating ownership.

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

Sidebar

Related Questions

I have a directed acyclic graph (DAG) with a weight associated with each node.
I have a directed acyclic graph implemented by Graph and Node classes. Each node
I have a directed-graph, each node is a complex data type. Does anyone know
I have a d3 force-directed graph that contains a group of nodes: var node
I have an acyclic directed graph. I would like to assign levels to each
I have a directed acyclic graph with nodes that are lists of entries that
I have a large (100,000+ nodes) Directed Acyclic Graph (DAG) and would like to
This is a supervised learning problem. I have a directed acyclic graph (DAG). Each
I have a D3 force directed graph which is first displayed with a few
I have a directed, positive weighted graph. Each edge have a cost of use.

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.