473,756 Members | 9,160 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Extending stl::list???

G'Day folks,

Have been working on this problem for quite some time and still no farther
ahead. :(

Here is my problem...bare with me i am very green :)

I have to implement a Safe List, that is derived from the STL class List and
uses exception handling.

From the various FAQ's and newsgroups it says that deriving from STL
containers is not wise due to the lack of virtual destructors.

Q1.) What is the purpose of this exercise if it is deemed to be poor
programming practice?

What i invision is a large object (SafeList) that contains my iterators,
stl::list and the various methods to work on the list The user
constructs/destroys this object
in a safe and efficient manner.

Q2.)Could you create a wrapper class for STL::List<> and wrap all you
iterators and any mutating methods that could produce memory error?...

// safeList.h

#ifndef SAFELIST_H
#define SAFELIST_H

#include <list>
#include <iterator>
#include <memory>
#include <stdexcept>
using namespace std;

typedef list<T> S;
typedef list<T>::iterat or SI;

template<typena me T>
class SafeList : private S {
public:
explicit SafeList(const S*);//No no-arg ctor
virtual ~SafeList();
S* operator->() throw(std::runt ime_error);
S* operator*() throw(std::runt ime_error);
//methods ..... //
class SafeIterator {
public:
SafeIterator(co nst SI*);
virtual ~SafeIterator() ;
SI* operator->() throw (std::runtime_e rror);
SI* operator*() throw (std::runtime_e rror);
private:
SI* _mySafeIterator ;
SafeIterator(co nst SafeIterator&);
SafeIterator& operator=(const SafeIterator&);
void operator new(size_t) throw (std::bad_alloc );
void operator delete(void*) throw();
;
private:
S* _mySafeList;
SafeList(const SafeList&);
SafeList& operator = (const SafeList&);
;

any help greatly appreciated

Barry

Jul 23 '05 #1
2 2407
"Barry Hynes" <hy****@mar.d fo-mpo.gc.ca> wrote in message
news:uB******** **************@ ursa-nb00s0.nbnet.nb .ca...
G'Day folks,

Have been working on this problem for quite some time and still no farther
ahead. :(

Here is my problem...bare with me i am very green :)

I have to implement a Safe List, that is derived from the STL class List and uses exception handling.

From the various FAQ's and newsgroups it says that deriving from STL
containers is not wise due to the lack of virtual destructors.

Q1.) What is the purpose of this exercise if it is deemed to be poor
programming practice?
I'm guessing that this is an assignment? If you're going to be graded on
this, then I suggest doing what the instructor told you to do, even if it is
considered bad practice. Often times, instructors don't care so much about
the "proper" way of doing things for their assignments. However, you are
right, public inheritance from standard containers is usually not preferred
because a) they don't have virtual destructors and b) the container is
publicly accessible, exposing implementation details. For example, one
could easily bypass your "safe" member functions by simply accessing the
std::list directly, giving them direct access to "unsafe" functionality that
could cause undefined behavior and preventing you from easily changing the
way you store elements inside SafeList. This isn't much of a problem with
private inheritance.
What i invision is a large object (SafeList) that contains my iterators,
stl::list and the various methods to work on the list The user
constructs/destroys this object
in a safe and efficient manner.

Q2.)Could you create a wrapper class for STL::List<> and wrap all you
iterators and any mutating methods that could produce memory error?...
Assuming you mean std::list, I don't see why not.
// safeList.h

#ifndef SAFELIST_H
#define SAFELIST_H

#include <list>
#include <iterator>
#include <memory>
#include <stdexcept>
using namespace std;

typedef list<T> S;
typedef list<T>::iterat or SI;

template<typena me T>
class SafeList : private S {
public:
explicit SafeList(const S*);//No no-arg ctor
virtual ~SafeList();
S* operator->() throw(std::runt ime_error);
S* operator*() throw(std::runt ime_error);
//methods ..... //
class SafeIterator {
public:
SafeIterator(co nst SI*);
virtual ~SafeIterator() ;
SI* operator->() throw (std::runtime_e rror);
SI* operator*() throw (std::runtime_e rror);
private:
SI* _mySafeIterator ;
SafeIterator(co nst SafeIterator&);
SafeIterator& operator=(const SafeIterator&);
void operator new(size_t) throw (std::bad_alloc );
void operator delete(void*) throw();
;
private:
S* _mySafeList;
SafeList(const SafeList&);
SafeList& operator = (const SafeList&);
;

any help greatly appreciated

The class template doesn't compile and has a few issues (for example, why
would you have a member list and inherit from it as well?), but I'm sure you
were just sketching out an idea in pseudo-C++. It's certainly feasible.
However, as I said before, if this is an assignment, follow the instructions
from the assignment.

--
David Hilsee
Jul 23 '05 #2

"David Hilsee" <da************ *@yahoo.com> wrote in message
news:DO******** ************@co mcast.com...
"Barry Hynes" <hy****@mar.d fo-mpo.gc.ca> wrote in message
news:uB******** **************@ ursa-nb00s0.nbnet.nb .ca...
G'Day folks,

Have been working on this problem for quite some time and still no
farther
ahead. :(

Here is my problem...bare with me i am very green :)

I have to implement a Safe List, that is derived from the STL class List and
uses exception handling.

From the various FAQ's and newsgroups it says that deriving from STL
containers is not wise due to the lack of virtual destructors.

Q1.) What is the purpose of this exercise if it is deemed to be poor
programming practice?


I'm guessing that this is an assignment?

yes

If you're going to be graded on this, then I suggest doing what the instructor told you to do, even if it
is
considered bad practice. this is thru correspondence

Often times, instructors don't care so much about the "proper" way of doing things for their assignments. However, you are
right, public inheritance from standard containers is usually not
preferred
because a) they don't have virtual destructors and b) the container is
publicly accessible, exposing implementation details. For example, one
could easily bypass your "safe" member functions by simply accessing the
std::list directly, giving them direct access to "unsafe" functionality
that
could cause undefined behavior and preventing you from easily changing the
way you store elements inside SafeList. This isn't much of a problem with
private inheritance.
What i invision is a large object (SafeList) that contains my iterators,
stl::list and the various methods to work on the list The user
constructs/destroys this object
in a safe and efficient manner.

Q2.)Could you create a wrapper class for STL::List<> and wrap all you
iterators and any mutating methods that could produce memory error?...
Assuming you mean std::list, I don't see why not.
// safeList.h

#ifndef SAFELIST_H
#define SAFELIST_H

#include <list>
#include <iterator>
#include <memory>
#include <stdexcept>
using namespace std;

typedef list<T> S;
typedef list<T>::iterat or SI;

template<typena me T>
class SafeList : private S {
public:
explicit SafeList(const S*);//No no-arg ctor
virtual ~SafeList();
S* operator->() throw(std::runt ime_error);
S* operator*() throw(std::runt ime_error);
//methods ..... //
class SafeIterator {
public:
SafeIterator(co nst SI*);
virtual ~SafeIterator() ;
SI* operator->() throw (std::runtime_e rror);
SI* operator*() throw (std::runtime_e rror);
private:
SI* _mySafeIterator ;
SafeIterator(co nst SafeIterator&);
SafeIterator& operator=(const SafeIterator&);
void operator new(size_t) throw (std::bad_alloc );
void operator delete(void*) throw();
;
private:
S* _mySafeList;
SafeList(const SafeList&);
SafeList& operator = (const SafeList&);
;

any help greatly appreciated

The class template doesn't compile and has a few issues (for example, why
would you have a member list and inherit from it as well?),

i have no idea...not sure what i am trying to do

but I'm sure you were just sketching out an idea in pseudo-C++. yes

It's certainly feasible. However, as I said before, if this is an assignment, follow the
instructions
from the assignment. here is the original question

pg 427
Implement a Safe List, that is derived from the STL class List and
uses exception handling.


--
David Hilsee


thanks for the help

Barry
Jul 23 '05 #3

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

Similar topics

12
10947
by: Brett L. Moore | last post by:
Hi, I have had trouble determining whether the STL list.size() operation is O(1) or O(n). I know the list is a doubly-linked list, so if the size() operation begins at the head, then counts to the end - obviously its O(n). However, keeping track of inserts and deletes isn't that tough, so its conceivable (to me!) that size() could be O(1). For my application, the lists can be quite long (millions of elements), so the answer greatly...
6
1648
by: JustSomeGuy | last post by:
I have an stl list that grows to be too huge to maintain effectivly in memory. There are elements within the list that could be stored on disk until accessed. However I don't want to expose this to the application class. How can I extent the stl list to write some elements to disk when they are put in the list and read them from disk when they are read from the list.
4
2881
by: Christian Christmann | last post by:
Hi, I need an STL list and was thinking of putting the list in wrapper class. The reason for my decision is that you can much better perform consistency checks. For instance, I need a function to append a list to another. Using a wrapper class I could do something like:
6
6891
by: Allerdyce.John | last post by:
Hi, How can I Random access an element in a STL List if I have a pointer to that list? I know how to do that if I have a reference to a STL list, but how can I do that if I have a pointer to a STL List. void function (vector<int>& myLIst) { // access the first element of myList printf ("%d", myList);
6
2963
by: Jonathan | last post by:
Hi. I'm having trouble figuring out what I should be doing here. I'm trying to remove an object from a list. The function is: void Alive::FromRoom () { list<Alive>::iterator iter = room->living.begin(); while (iter != room->living.end()) {
5
13024
by: silverburgh.meryl | last post by:
is there a better way to compare 2 stl list? I write a function like this below, but I wonder if there is a better way to achieve that (e.g. less code)? bool isSame(const list<int>& srcList, const list<int>& destList ) { if (srcList.size() != destList.size()) { return false; }
2
1753
by: giotheninman | last post by:
I have a stl list and i have a problem allocating memory for it. I am using the malloc operator in order to get memory for it and I have tried using placement new but it still isnt setting up the list correctly. The solution im looking for is something as follows myObj->std::list<tObj= (std::list<tObj> *)malloc(sizeof(std::list<tObj>)); SetupListWithoutGettingNewMemory(myObj->std::list<tObj>);
3
2725
by: Christian Christmann | last post by:
Hi, reading the output of gprof for one of my projects, I found that the STL list assignment operator consumes a larger fraction of the program's execution time. The exact entry in gprof's output looks as follows: std::list<MyClass*, std::allocator<MyClass*::operator= (std::list<MyClass*, std::allocator<MyClass* const&) >
27
2767
by: Jason Doucette | last post by:
I'm getting an assertion fire from a list iterator being checked against NULL. This did not occur in VC++ 2003 (v7.1). Are there changes that have been made to the STL between these versions that I should know about? Any resources I should check into? thanks, Jason
0
9292
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10062
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9728
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8733
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5322
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3827
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3392
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2694
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.