std::list inheritance
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: std::list inheritance

  1. #1
    Join Date
    Sep 2003
    Location
    Boston, Ma
    Posts
    52

    std::list inheritance

    Hello all,
    I need to write a derived class of stl list class so I did all the studying and research but now as I just started I get some weird error messages that make me think I probably got the wrong syntax.
    Here is my code
    Code:
    #include<list>
    #include <algorithm>
    
    using namespace std;
    
    template<class TYPE>
    class OrderedList<TYPE> : public list<TYPE> {
      public:
        void Promote_member();
    };
    template<class TYPE> void OrderedList<TYPE>::Promote_member(){}
    The error messages I get are the following
    Code:
      `OrderedList'  is not a template 
    can't make ` Promote_member' into a method -- not in a class
    syntax error  before `::' token
    ISO C++ forbids declaration of `Promote_member' with no type
    So I was hoping that somebody can point out what I did wrong in the above as I need to write three more child classes of the list class.
    Thanks for any help.
    Regards,
    Sofia.

  2. #2
    Join Date
    Apr 2000
    Location
    Frederick, Maryland
    Posts
    507
    You should not inherit class from STL Container. Take a look at one of my article for discussing this question.

    http://www.codeguru.com/cpp_mfc/FinalClass.html

    You should try to use the power of template instead of doing inheritance.

    Hope it helps.

  3. #3
    Join Date
    Sep 2003
    Location
    Boston, Ma
    Posts
    52
    I don't see how I can achieve what I'm aiming to do using templates.
    I need to write several child classes of the list class that would have the same functionality as std::list and extra ones that I would define(I don't feel like reinventing the wheel).
    one example of the derived classes I need to write is a self-organizing list(I figured out how to do that one except for getting rid of the error messages mentioned in the post above).
    Thanks for your help though.
    Sofia.

  4. #4
    Join Date
    Apr 2000
    Location
    Frederick, Maryland
    Posts
    507
    You don not have to give template parameter when defining the class.

    Try this

    Code:
    template<class TYPE>
    class OrderedList : public list<TYPE> 
    {
      public:
    	  void Promote_member();
    };
    
    template<class TYPE> void OrderedList<TYPE>::Promote_member(){}
    And for self organized list (i guess orderd list) you have other choices like std::set or std::multiset. std::list also has sort member function which you can use to sort it. I still can understand why you want to inherit class from std::list.

    Hope it helps.

  5. #5
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    We should really put that into the FAQ

    The reason you should not derive from any of the STL containers is that they are not built for inheritance. For example, they don't have a virtual destructor, which is the minimum for a class which can be derived from.

    If you want to build a new container which uses functionality of std::list, then you have to use containment like in the following skeleton:
    Code:
    template <class T>
    class MyList
    {
      std::list<T>  m_elems;
    public:
      MyList() : m_elems() {}
      // other details
    };
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  6. #6
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    why do you explicitley include m_elems() in the intializer list?
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  7. #7
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    In this example, for no reason The thing is that it has become a habit because many times I want to make the construction of a contained object not the default one, and for that purpose it's better to do it like this rather than invoking the contained object's default constructor and then changing it again during my constructor.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  8. #8
    Join Date
    Sep 2003
    Location
    Boston, Ma
    Posts
    52
    Originally posted by Zeeshan
    You don not have to give template parameter when defining the class.

    Try this

    Code:
    template<class TYPE>
    class OrderedList : public list<TYPE> 
    {
      public:
    	  void Promote_member();
    };
    
    template<class TYPE> void OrderedList<TYPE>::Promote_member(){}
    Thanks Zeeshan, that solved my problem. It's weird though as I'm looking at a book(Data structures in C++ using STL"Timothy Budd") right now that implements it the way I was trying at first.
    And for self organized list (i guess orderd list) you have other choices like std::set or std::multiset. std::list also has sort member function which you can use to sort it. I still can understand why you want to inherit class from std::list.
    I have to use a class derived from stl list as that's what the specs say

  9. #9
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Originally posted by megabyte
    I have to use a class derived from stl list as that's what the specs say
    Well...then I would simply say the specification is invalid and cannot be fulfilled. I do not know whether this is a school assignment or not, however, the answer to this assignment is simply: There is no way of doing that unless you want to introduce errors/wrong behaviour into your code...
    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds


    Article(s): Allocators (STL) Function Objects (STL)

  10. #10
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    Andreas,

    I know it doesn't make much sense to derive from a class that does not have a virtual destructor, but do you have any quotes of "famous" people like Bjarne, Sutter or the like who explicitly say that you should not derive from stl containers ? That may help to convince people
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  11. #11
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    How about

    If you derive from an STL container, then I am going to kick your ***.
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  12. #12
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    Nah, that would not work, people would just continue doing it sneakily behind our backs
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  13. #13
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Originally posted by Yves M
    Andreas,

    I know it doesn't make much sense to derive from a class that does not have a virtual destructor, but do you have any quotes of "famous" people like Bjarne, Sutter or the like who explicitly say that you should not derive from stl containers ? That may help to convince people
    Well...not handy at the moment...but it would be indeed a good reason...
    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds


    Article(s): Allocators (STL) Function Objects (STL)

  14. #14
    Join Date
    Sep 2002
    Posts
    1,747
    I've seen almost all the "experts" expound the horrors of deriving from the standard containers over on comp.lang.c++.moderated. Its the standard response given for any questions that request deriving from the containers, and there is very good reason. No one wants to derive from these containers so they can access the internal data members, and no one wants to use inheritance to reimplement virtual functions (there are none) -- and those are the two reasons one would desire Liskov substitution. Everyone who ever mentions using inheritance on the standard containers always mentions that they desire to extend the functionality. Almost always, the correct way to do this is to just write a standalone algorithm function that operates either on iterators or the class itself. If the programmer wants to add data members, they likely want a completely new class that just contains the standard container along with the other data members. If they want to expose the functionality of the data members, they can just make the object a data structure (which is what it is if they are exposing the implementation details). There is never a good reason to derive from the standard containers, and lots of good reasons not to.
    */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/

    "It's hard to believe in something you don't understand." -- the sidhi X-files episode

    galathaea: prankster, fablist, magician, liar

  15. #15
    Join Date
    Apr 1999
    Posts
    27,449
    Originally posted by megabyte
    I have to use a class derived from stl list as that's what the specs say
    I agree with Andreas in that the specs that you are referring to are going to lead to trouble if not careful.

    The STL container classes are not base classes, plain and simple -- they are meant as building blocks. Also, there is no virtual destructor, so any code that comes on down that uses your classes polymorphically will be doomed for failure.
    Code:
    #include <string>
    class MyOrderedList : public OrderedList
    {
    public:
          virtual ~MyOrderedList() { }
          virtual std::string GetName() { return "NoName"; }
    };
    
    class MyOrderedListOfCars : public MyOrderedList
    {
    public:
          ~MyOrderedListOfCars() { }
           std::string GetName() { return "Cars"; }
    };
    
    class MyOrderedListOfHouses : public MyOrderedList
    {
    public:
          ~MyOrderedListOfHouses() { }
           std::string GetName() { return "Houses"; }
    };
    
    void foo()
    {
       MyOrderedList *pList;
       if ( the_user_wants_cars )
          pList = new MyOrderedListOfCars;
       else
          pList = new MyOrderedListOfHouses;
       std::string sName = pList->GetName();
    
       delete pList;  // undefined behavior!
    }
    Even though MyOrderedList has a virtual destructor, the code still fails because the base class, std::list<>, has no virtual destructor.

    It isn't enough to say "I don't write code like this". You never know what code you will write in the future, or how someone else may use the OrderedList class in the future.

    Regards,

    Paul McKenzie

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


HTML5 Development Center