STL derived class : parse list from inside

Discussion in 'C++' started by Vincent RICHOMME, Nov 8, 2006.

  1. Last question before to go to sleep :

    let's say I have a class derived from std::list

    template<typename T>
    class List : public std::list<T>
    {
    static bool const is_ptr = is_pointer<T>::value;

    public:

    ~List() { Clear(); }

    inline void Add(T object) { push_back(object); }

    inline void Clear(bool bDeallocate = true)
    {
    if (bDeallocate && is_ptr)
    {
    // CODE BELOW WORKS WITH A VECTOR - FOR A LIST
    /*int count = this->size();
    while ( count ){
    delete this->operator[]( --count );
    }*/
    }
    this->std::list<T>::clear();
    }

    inline int GetCount() { return size(); }
    };

    How can I parse my list from my derived class?
    Vincent RICHOMME, Nov 8, 2006
    #1
    1. Advertising

  2. Vincent RICHOMME

    Salt_Peter Guest

    Vincent RICHOMME wrote:
    > Last question before to go to sleep :
    >
    > let's say I have a class derived from std::list
    >
    > template<typename T>
    > class List : public std::list<T>
    > {
    > static bool const is_ptr = is_pointer<T>::value;
    >
    > public:
    >
    > ~List() { Clear(); }
    >
    > inline void Add(T object) { push_back(object); }
    >
    > inline void Clear(bool bDeallocate = true)
    > {
    > if (bDeallocate && is_ptr)
    > {
    > // CODE BELOW WORKS WITH A VECTOR - FOR A LIST
    > /*int count = this->size();
    > while ( count ){
    > delete this->operator[]( --count );
    > }*/
    > }
    > this->std::list<T>::clear();
    > }
    >
    > inline int GetCount() { return size(); }
    > };
    >
    > How can I parse my list from my derived class?


    Its often not a good idea to inherit from standard library containers.
    These do not have a virtual destructors. I'ld suggest simply making the
    std::list a member. If you need to encapsulate the list privately,
    follow the interface that the std::list provides (consult <list>)
    Instead of using new/delete allocations, why not use smart pointers?
    I'ld *strongly* recommend boost's shared_ptr for the task. Lets create
    a dummy class N to track the auto-deallocation of the elements in the
    std::list below:

    #include <iostream>
    #include <ostream>
    #include <list>
    #include <iterator>
    #include <boost/shared_ptr.hpp>

    /* dummy class */
    struct N
    {
    N() : n(0) { std::cerr << "N()\n"; }
    ~N() { std::cerr << "~N()\n"; }
    friend std::eek:stream&
    operator<<(std::eek:stream& os, const N& r_n)
    {
    return os << r_n.n;
    }
    private:
    int n;
    };

    template< typename T >
    struct List
    {
    std::list< T > thelist;
    };

    /* global op<< overload */
    template< typename T >
    std::eek:stream&
    operator<<(std::eek:stream& os, const List< boost::shared_ptr< T > >& r_l)
    {
    typedef boost::shared_ptr< T > BspT;
    typedef typename std::list< BspT >::const_iterator LTIter;
    for(LTIter it = r_l.thelist.begin(); it != r_l.thelist.end(); ++it)
    {
    os << *(*it) << "\n";
    }
    return os;
    }

    int main()
    {
    /* typedef boost::shared_ptr */
    typedef boost::shared_ptr< N > SharedPtrStr;
    List< SharedPtrStr > slist;
    slist.thelist.push_back( SharedPtrStr(new N) );
    slist.thelist.push_back( SharedPtrStr(new N) );
    slist.thelist.push_back( SharedPtrStr(new N) );

    std::cout << "slist size = ";
    std::cout << slist.thelist.size() << std::endl;

    std::cout << slist;
    } // automatic deallocation happens here

    /*
    N()
    N()
    N()
    slist size = 3
    0
    0
    0
    ~N() // <- smart deallocations
    ~N()
    ~N()
    */
    Salt_Peter, Nov 9, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    394
    myork
    May 23, 2007
  2. Replies:
    1
    Views:
    385
    Victor Bazarov
    May 23, 2007
  3. Replies:
    3
    Views:
    339
  4. Goran Djuranovic

    Retrieving derived class name inside base class

    Goran Djuranovic, Sep 10, 2007, in forum: ASP .Net
    Replies:
    8
    Views:
    433
    Goran Djuranovic
    Sep 17, 2007
  5. David
    Replies:
    3
    Views:
    401
    Grizlyk
    Jan 29, 2008
Loading...

Share This Page