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

  • SEARCH
  • Home
  • 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 8927531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:14:21+00:00 2026-06-15T08:14:21+00:00

As I understand it, a unique_ptr signifies exclusive ownership. A singly linked list seems

  • 0

As I understand it, a unique_ptr signifies exclusive ownership. A singly linked list seems to fit this, with each node owning the next, like (pseduocode alert)

class node{
public:
      unique_ptr<node> next;
      int value;
};

but I don’t understand how to perform operations like traversing the list, where I’m used to doing

here=here->next;

How do you implement data structures using unique_ptr‘s? Are they the right tool for the job?

  • 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-15T08:14:23+00:00Added an answer on June 15, 2026 at 8:14 am

    When you go through the nodes, you don’t need to own the node pointer, which means that

    here=here->next;

    Is incorrect if here is a unique_ptr.
    Owning an object means “being responsible for it’s life and death” which means the owner is the one who have the code that will destroy the object. If you use another definition of owning, then it’s not what unique_ptr means.

    In you’re list node code, you assume that each node is responsible for the next node (if you destroy a node, all the next nodes will be destroyed too). It can be valid behaviour, it depends on your needs, just be sure it’s what you really wants.

    What you want is to read the pointer without owning it. Current good practice to do this is to use a raw pointer indicating a “use but don’t own” kind of usage to other developers looking at this code (unique_ptr means “if I die, the pointed object dies too”):

    node* here = nullptr; // it will not own the pointed nodes (don't call delete with this pointer)
    here = &first_node(); // assuming first_node() returns a reference to the first node
    here = here->next.get(); // to get the next node without owning it: use get() - true in all smart pointers interface
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I understand this is an easy question but for some reason this just isn't
I understand I can create an enum like this: public enum MyEnum { ONE(1),
Consider this code: using namespace std; int* get() { unique_ptr<int> p (new int[4]); return
As I understand it when Git assigns a SHA1 hash to a file this
I understand we do need an unique APP ID, but seems I have tested
I'm trying to understand what this query does exactly: SELECT DISTINCT `state`, `state_name` FROM
I understand what tapply() does in R. However, I cannot parse this description of
I'm looking at [VC10's] unique_ptr and they do a couple things I don't understand:
I understand how str_shuffle() or shuffle works but I don't know it in this
As I understand it, in the current specification of C++11, one should use: std::unique_ptr<>

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.