Can you make an std::list of a templated class.

This is a discussion on Can you make an std::list of a templated class. within the C++ Programming forums, part of the General Programming Boards category; Quick question. I was wondering if I had a class that was template would I be able to use that ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    131

    Can you make an std::list of a templated class.

    Quick question.

    I was wondering if I had a class that was template would I be able to use that class as the type for an std::list?

    i.e.

    Code:
    template <class T>
    class TemplatedClass
    {
    public:
    
    private:
        T var;
    } ;
    
    
    std::list< TemplatedClass > list;
    Regards

    Chad

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,702
    You have to specify T for TemplatedClass in order to instantiate a std::list.

    > std::list<TemplatedClass<int> > my_list;

    gg

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    So the actual type of the std::list has to be defined? Darn. Looks like I have to redesign some things.

    Regards

    Chad

  4. #4
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    another alternative is:

    Code:
    class abstractClass
    {
    };
    template <class T>
    class TemplatedClass : public abstractClass
    {
    public:
    
    private:
        T var;
    } ;
    
    
    std::list< abstractClass* > list;

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    Quote Originally Posted by m37h0d View Post
    another alternative is:

    Code:
    class abstractClass
    {
    };
    template <class T>
    class TemplatedClass : public abstractClass
    {
    public:
    
    private:
        T var;
    } ;
    
    
    std::list< abstractClass* > list;

    I thought std::list would splice off the deriving class.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Quote Originally Posted by chadsxe
    I thought std::list would splice off the deriving class.
    If you mean type slicing, rather than list splicing, then no, since a base class pointer is stored. On the other hand, Codeplug's answer sounds more relevant since you mentioned nothing that would make it seem like inheritance was applicable.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    i'm inferring from the OP that he needs to store different types in the list. if this assumption is correct what other alternatives are there?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Quote Originally Posted by m37h0d
    i'm inferring from the OP that he needs to store different types in the list.
    Maybe, but the original question seemed like a simple novice's mistake. Considering chadsxe's post #3 though, it could be that chadsxe actually wants to define a function template or another class template, or maybe you really are correct, or...

    Quote Originally Posted by m37h0d
    if this assumption is correct what other alternatives are there?
    boost::any is an alternative.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    Given m37h0d example would there worries of memory managment in regards to the pointers being stored in the list. Would erasing of elements in the list handle the destroying of the pointers or would there be an issue there.

    Regards

    Chad

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Quote Originally Posted by chadsxe
    Given m37h0d example would there worries of memory managment in regards to the pointers being stored in the list. Would erasing of elements in the list handle the destroying of the pointers or would there be an issue there.
    If the linked list is supposed to own the objects pointed to, then yes, you have to do a little more work to implement RAII for the object that owns the linked list. Certainly, just erasing the elements of the list would not destroy the objects that those pointers point to.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,022
    Usually, that can be solved with smart pointers.
    std::tr1::shared_ptr or boost::shared_ptr.
    Don't be afraid of it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    if the base class has a virtual destructor then you can iterate over the container and delete each element to properly free the derived class instances.

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    I have never used boost so excuse me if I ask a dumb question. In regards to boost::any does it only deal with c++ types i.e. flot string bool etc. Can it handle something like a D3DCOLORVALUE.

    In my example I would do something to the effect of that correct.

    std::list<boost::any> list;

    Regards

    Chad

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,022
    boost::any doesn't care what type it deals with. It is designed to work with any and every type.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    Quote Originally Posted by Elysia View Post
    boost::any doesn't care what type it deals with. It is designed to work with any and every type.
    Then this appears to me to be the cleanest way of doing this. Do you agree. I am guessing the code would go something like this.

    Code:
    #include <boost/any.hpp>
    template <class T>
    class TemplatedClass
    {
    public:
    
    private:
        T var;
    } ;
    
    
    std::list< boost::any > list;
    What do you think?

    Regards

    Chad
    Last edited by chadsxe; 02-12-2009 at 08:45 AM.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Overloading templated class operators
    By Artemis0583 in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2006, 12:30 PM
  3. Need help to build network class
    By weeb0 in forum C++ Programming
    Replies: 0
    Last Post: 02-01-2006, 11:33 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Difficulty superclassing EDIT window class
    By cDir in forum Windows Programming
    Replies: 7
    Last Post: 02-21-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21