473,395 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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?
Nov 8 '06 #1
1 2263

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

Nov 9 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Anand | last post by:
class base: def __setattr__(self,attr,key,*unexpected): print "Base Class :",attr,key,unexpected,self.__dict__ self.__dict__ = key def __getattr__(self,attr,*unexpected): print "Base Class...
1
by: Bren | last post by:
I'm hoping somebody can help me with the following problem. I'm hoping it's not insurmountable. Here's a basic structure: class EventTarget { public: EventTarget(); virtual ~EventTarget();
24
by: Shao Zhang | last post by:
Hi, I am not sure if the virtual keyword for the derived classes are required given that the base class already declares it virtual. class A { public: virtual ~A();
1
by: Piotr Sawuk | last post by:
just a quick question out of curiosity: how to initialize const arrays? struct srat { long num; ulong den; srat(){} } struct collisions
3
by: Kirk Marple | last post by:
Just want to see if this is 'by design' or a bug... I have a common List<T> defined in a base class, and the base class has a static property to expose this list. I wanted the derived class to...
6
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by...
10
by: campos | last post by:
"Effective C++ 3rd Edition" Item 6, P39 ------------------------------------------------------- class Uncopyable { protected: // allow construction Uncopyable() {} // and...
3
by: Goran Djuranovic | last post by:
Hi all, Is there a way to retrieve a derived class name inside a subroutine or a function of the base class? I am trying to get some data from the database, based on which derived class is calling...
1
by: John M. King | last post by:
Hi, I have the following code structure, where the base class's constructor takes a SomeClass pointer and stores it internally in a member. Each derived class needs to have access to that...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.