Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Linked Lists and unique_ptr's

Linked Lists and unique_ptr's

Scheduled Pinned Locked Moved C / C++ / MFC
c++javadata-structureshelpquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tom Moore
    wrote on last edited by
    #1

    Hi, its been a while since I've used CodeProject. Quick Question. I'm training myself on standard algorithms and trying to create a linked list. In Java it's easy.

    class Node{
    public int data;
    public Node next;
    public Node(int val){ data= val; next = null;}
    }

    In the C++ Pre Ox11 you could use raw pointers.

    struct Node{
    int data;
    Node* next;

    Node(int val):data(val),next(0){}
    }

    but you had to new up your next node and delete it afterwards. With the new version of C++, you can use unique_ptr. But if I try to use that, I can't seem to get a good node to next.

    struct node{
    int data;
    unique_ptr next;

    node(int al):data(val),next(nullptr){}
    }
    node a(3);
    node b(4);
    a.next = b; // Error
    a.next = move(b); // Error
    a.next.reset (b); // Error

    Please could you enlighten me, on how I can get this to work. Many Thanks Tom

    E C 2 Replies Last reply
    0
    • T Tom Moore

      Hi, its been a while since I've used CodeProject. Quick Question. I'm training myself on standard algorithms and trying to create a linked list. In Java it's easy.

      class Node{
      public int data;
      public Node next;
      public Node(int val){ data= val; next = null;}
      }

      In the C++ Pre Ox11 you could use raw pointers.

      struct Node{
      int data;
      Node* next;

      Node(int val):data(val),next(0){}
      }

      but you had to new up your next node and delete it afterwards. With the new version of C++, you can use unique_ptr. But if I try to use that, I can't seem to get a good node to next.

      struct node{
      int data;
      unique_ptr next;

      node(int al):data(val),next(nullptr){}
      }
      node a(3);
      node b(4);
      a.next = b; // Error
      a.next = move(b); // Error
      a.next.reset (b); // Error

      Please could you enlighten me, on how I can get this to work. Many Thanks Tom

      E Offline
      E Offline
      Eugen Podsypalnikov
      wrote on last edited by
      #2

      Hm... Try it :) :

      node a(3); // a, will be deleted as a stack variable
      a.next = new node(4); // b, will be deleted by unique_ptr of a
      a.next->next = new node(5); // c, will be deleted by unique_ptr of b
      // 1. deleting of b, not "tested" :) :
      a.next = move(a.next->next); // is there a crash ?
      // 2. then deleting of b, old style :) :
      unique_ptr temp = move(a.next->next);
      a.next = move(temp);

      PS: any pre 0x11 C++ strcture or class may have a destructor as well :

      struct node {
      int m_iData;
      node* m_pNext;

      node(int iData) : m_iData(iData), m_pNext(NULL) {}
      ~node() { delete m_pNext; }

      node* detach() { node* prevNext(m_pNext); m_pNext = NULL; return prevNext; }
      } a(3);

      They sought it with thimbles, they sought it with care; They pursued it with forks and hope; They threatened its life with a railway-share; They charmed it with smiles and soap. :)

      1 Reply Last reply
      0
      • T Tom Moore

        Hi, its been a while since I've used CodeProject. Quick Question. I'm training myself on standard algorithms and trying to create a linked list. In Java it's easy.

        class Node{
        public int data;
        public Node next;
        public Node(int val){ data= val; next = null;}
        }

        In the C++ Pre Ox11 you could use raw pointers.

        struct Node{
        int data;
        Node* next;

        Node(int val):data(val),next(0){}
        }

        but you had to new up your next node and delete it afterwards. With the new version of C++, you can use unique_ptr. But if I try to use that, I can't seem to get a good node to next.

        struct node{
        int data;
        unique_ptr next;

        node(int al):data(val),next(nullptr){}
        }
        node a(3);
        node b(4);
        a.next = b; // Error
        a.next = move(b); // Error
        a.next.reset (b); // Error

        Please could you enlighten me, on how I can get this to work. Many Thanks Tom

        C Offline
        C Offline
        Chris Meech
        wrote on last edited by
        #3

        I'm just guessing but the statement

        a.next = b;

        looks like it should generate some kind of type mismatch error. At the very least I think you would have to deliberately cast 'b' to something before you can assign it to a unique_ptr variable. I'm not sure about the other two errors. :)

        Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups