Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   STL derived class : parse list from inside (http://www.velocityreviews.com/forums/t458273-stl-derived-class-parse-list-from-inside.html)

Vincent RICHOMME 11-08-2006 11:54 PM

STL derived class : parse list from inside
 
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?

Salt_Peter 11-09-2006 02:10 AM

Re: STL derived class : parse list from inside
 

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::ostream&
operator<<(std::ostream& 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::ostream&
operator<<(std::ostream& 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()
*/



All times are GMT. The time now is 10:57 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.