STL derived class : parse list from inside

  • Thread starter Vincent RICHOMME
  • Start date
V

Vincent RICHOMME

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?
 
S

Salt_Peter

Vincent said:
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()
*/
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top