Copying objects between pointers and references C++

101 Views Asked by At

I have done some reading starting on the Rule of three and this has given me some exposure to memory management when writing C++ and it is challenging coming from a Java background.

I'm just writing some toy programs to begin with and just wanting to construct a basic linked list.

I have my Node:

class Node
{
   public:
      std::string des;
      int val;
      Node *nxt;
      Node();
      Node(std::string d, int v) : des(d), val(v);
      ~Node();
   private:    

}

And my Linked List:

class LinkedList
{
   public:
      Node *hd;
      Node *tl;
      LinkedList() : hd=nullptr, tl=nullptr;
      void Append(const Node &nod)
      {
         hd=nod; 
      }
      ~LinkedList();
   private:

}

I wanted to write the code like this going from what I have learned so far.

int main(void)
{
   
   std::cout << "Create some objects on the stack." << std::endl;
   LinkedList m_ls();
   Node m_nod1("first node", 30);
   Node m_nod1("second node", 36);
   Node m_nod1("third node", 42);
   
   m_ls.Append(m_nod1);
   m_ls.Append(m_nod2);
   m_ls.Append(m_nod3);

}

I am getting an obvious compiler warning because my assignment of this->hd=nod but it is not obvious to me how I'm to write this properly. Will casting between types and a copy-constructor solve this? Appreciate the help.

1

There are 1 best solutions below

0
Remy Lebeau On

To answer your specific question - to fix the compiler error, you would need to drop the const from your nod parameter, and then use the address-of operator& to get the memory address of the passed Node object when assigning it to the hd pointer, eg:

void Append(Node &nod)
{
   hd = &nod; 
}

However, this is not the correct way to write a linked list class (at least in this example). You should instead not let the user pass in Node objects at all, they should only pass in the data they want to add to the list, let the LinkedList class handle the Node objects internally as needed.

For example:

class Node
{
   public:
      std::string des;
      int val;
      Node *nxt = nullptr;
      Node(std::string d, int v) : des(d), val(v) {}
};

class LinkedList
{
   public:
      LinkedList() = default;

      ~LinkedList()
      {
         Node *nd = hd, *nxt;
         while (nd)
         {
            nxt = nd->nxt;
            delete nd;
            nd = nxt;
         }
      }

      void Append(std::string des, int val)
      {
         Node *n = new Node(des, val);
         if (!hd) hd = n;
         if (tl) tl->next = n;
         tl = n;
      }

      // implement these later during your Rule-of-3/5 studies...
      LinkedList(const LinkedList&) = delete;
      LinkedList(LinkedList&&) = delete;
      LinkedList& operator=(const LinkedList&) = delete;
      LinkedList& operator=(LinkedList&&) = delete;

   private:
      Node *hd = nullptr;
      Node *tl = nullptr;
};

int main()
{
   std::cout << "Create some objects in the list." << std::endl;

   LinkedList m_ls;
   m_ls.Append("first node", 30);
   m_ls.Append("second node", 36);
   m_ls.Append("third node", 42);
}