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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:52:53+00:00 2026-06-15T07:52:53+00:00

template <class T> class list { public: //stuff list(const list &cctorList); //cctor private: struct

  • 0
template <class T>
class list
{
public:
    //stuff
    list(const list &cctorList); //cctor
private:
    struct node
    {
       node *next;
       node *previous;
       T *info;
    }

    node *firstNode; //pointer to the first node (NULL if none)
    node *lastNode; //pointer to the last node (NULL if none)
}

I’m now trying to define list(const list &cctorList); //cctor but I’m running into trouble.

Here’s what I have so far:

template <class T>
list<T>::list(const list &cctorList)
{
    node *another = new node;
    firstNode = another;
    another->previous = NULL;
    another->info = new T(*(cctorList->info));

    // ...
}

Is everything up to this point correct? Is there a way for me to recursively assign another->next? Also, is there an easier way to accomplish this using iterators?

  • 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-15T07:52:54+00:00Added an answer on June 15, 2026 at 7:52 am
    1. You should be using std::list. In fact, you should be using std::vector, because it is faster for most practical purposes (list is only faster if the objects are really large or really expensive to construct) and you don’t need random access.

    2. new T(*(cctorList->info)); won’t compile, because cctorList (list&) does not have operator-> and it does not have info member either.

    3. The copy constructor is best implemented in terms of the other, more primitive operations like push_back and iteration. So first do those and than the copy constructor becomes:

      template <class T>
      list<T>::list(const list &cctorList)
      {
          std::copy(begin(cctorList), end(cctorList), std::back_inserter(this));
      }
      

      In fact I’d just template that constructor:

      template <class T, class Collection> list(const Collection &cctorList)
      

      (body remains the same). That works as copy constructor, but also allows copying from any other collection of any type that can be implicitly converted to T.

    4. The actual data should be held by value. I.e. the node should be defined as

      struct node
      {
          node *next;
          node *previous;
          T info;
      }
      

      you are copying the value anyway, so you don’t need to do two separate allocations for node and T when one will do.


    Edit: You say you want to learn concepts. But the most important concept of modern C++ is composition of algorithms. Their definitions are often trivial. Basic implementation of std::copy is just:

    template <typename InputIterator, typename OutputIterator>
    OutputIterator copy(InputIterator begin, InputIterator end, OutputIterator out) {
        for(;begin != end; ++out, ++begin) *out = *begin;
    }
    

    Now this does not appear to allocate anything. The trick lies in the back_insertion_iterator. Insertion iterator is a trick to make this work without preallocating the sequences. It defines operator* using push_back on the underlying collection and ignores operator++. That satisfies “output iterator” concept, because it only guarantees to work when these two calls are strictly interleaved and makes algorithms work on many things from plain old arrays to output streams.

    The other part is that while the trivial definitions are correct, they are not the actual definitions used in the library. The actual definitions in the library are optimized. E.g. usually std::copy will check whether the input iterators know their distance and if the output is insert operator to sequence with reserve operation and call it to avoid some allocations. Those are optimizations and depend on implementation details of the standard library.

    You can go and write down the basic implementations of things from standard library and test they work the same if you want to understand them. But you should follow the way standard library defines things by building them up from simple helper bits like std::copy, std::swap, insertion iterator adapters and such. If you look in the standard library, most functions there are one-liners!

    Edit2: Also with all the genericity the standard library provides, there are still bits criticized for not being generic enough. E.g. GotW #84: Monoliths “Unstrung” discusses which methods of std::string could be converted to generic algorithms.

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

Sidebar

Related Questions

I have a simple container : template <class nodeType> list { public: struct node
Here's my code: template<typename T> class list { private: node<T>* head; node<T>* tail; int
I have code that looks like this: template<class T> class list { public: class
Because I've overloaded the operator++ for an iterator class template<typename T> typename list<T>::iterator& list<T>::iterator::operator++()
I have made a template class for a node in a linked list and
When I try to use my iterator class template<class T> class list { public:
template <class T> class List { public: List(); ~List(); ... protected: template <class T>
template<typename Type> class List { public: List(void); ~List(void); ... } which inherited by template<typename
I have a custom iterator template class that wraps up a std::list iterator. In
I'm trying to create my own template for a List class as a learning

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.