Note that there are some explanatory texts on larger screens.

plurals
  1. POLinkedList copy constructor implementation details
    primarykey
    data
    text
    <p>I'm starting to learn C++ and as an exercise decide to implement a simple <code>LinkedList</code> class (Below there is part of the code). I have a question regarding the way the copy constructor should be implemented and the best way the data on the original <code>LinkedList</code> should be accessed.</p> <pre><code> template &lt;typename T&gt; class LinkedList { struct Node { T data; Node *next; Node(T t, Node *n) : data(t), next(n) {}; }; public: LinkedList(); LinkedList(const LinkedList&amp;); ~LinkedList(); //member functions int size() const; //done bool empty() const; //done void append(const T&amp;); //done void prepend(const T&amp;); //done void insert(const T&amp;, int i); bool contains(const T&amp;) const; //done bool removeOne(const T&amp;); //done int removeAll(const T&amp;); //done void clear(); //done T&amp; last(); //done const T&amp; last() const; //done T&amp; first(); //done const T&amp; first() const; //done void removeFirst(); //done T takeFirst(); //done void removeLast(); T takeLast(); //delete when finished void print(); //end delete //operators bool operator ==(const LinkedList&lt;T&gt; &amp;other) const; //done bool operator !=(const LinkedList&lt;T&gt; &amp;other) const; //done LinkedList&lt;T&gt;&amp; operator =(const LinkedList&lt;T&gt; &amp;other); //done private: Node* m_head; Node* m_tail; int m_size; }; template&lt;typename T&gt; LinkedList&lt;T&gt;::LinkedList() : m_head(0), m_tail(0), m_size(0) { } ... </code></pre> <p>Should my copy constructor access the data on each node of the original <code>LinkedList</code> directly?</p> <pre><code>template&lt;typename T&gt; LinkedList&lt;T&gt;::LinkedList(const LinkedList&amp; l) { m_head = 0; m_tail = 0; m_size = 0; Node *n = l.m_head; // construct list from given list while(n) { append(n-&gt;data); n = n-&gt;next; } } </code></pre> <p>Or should I access the data through the corresponding accessor? (I know that I don't have the accessor(s) defined).</p> <p>Also, I intend to create a custom iterator so that it can be possible to iterate over the <code>LinkedList</code>. Should I use in the copy constructor to access the data on each node? </p> <p>Another question (completely off-topic, I know), when and/or why should we declare a pointer to a <code>LinkedList</code></p> <pre><code>LinkedList&lt;int&gt; *l = new LinkedList&lt;int&gt;(); </code></pre> <p>instead of</p> <pre><code>LinkedList&lt;int&gt; l; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload