473,406 Members | 2,467 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,406 software developers and data experts.

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>::iterator SI;

template<typename T>
class SafeList : private S {
public:
explicit SafeList(const S*);//No no-arg ctor
virtual ~SafeList();
S* operator->() throw(std::runtime_error);
S* operator*() throw(std::runtime_error);
//methods ..... //
class SafeIterator {
public:
SafeIterator(const SI*);
virtual ~SafeIterator();
SI* operator->() throw (std::runtime_error);
SI* operator*() throw (std::runtime_error);
private:
SI* _mySafeIterator;
SafeIterator(const 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 2382
"Barry Hynes" <hy****@mar.dfo-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>::iterator SI;

template<typename T>
class SafeList : private S {
public:
explicit SafeList(const S*);//No no-arg ctor
virtual ~SafeList();
S* operator->() throw(std::runtime_error);
S* operator*() throw(std::runtime_error);
//methods ..... //
class SafeIterator {
public:
SafeIterator(const SI*);
virtual ~SafeIterator();
SI* operator->() throw (std::runtime_error);
SI* operator*() throw (std::runtime_error);
private:
SI* _mySafeIterator;
SafeIterator(const 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********************@comcast.com...
"Barry Hynes" <hy****@mar.dfo-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>::iterator SI;

template<typename T>
class SafeList : private S {
public:
explicit SafeList(const S*);//No no-arg ctor
virtual ~SafeList();
S* operator->() throw(std::runtime_error);
S* operator*() throw(std::runtime_error);
//methods ..... //
class SafeIterator {
public:
SafeIterator(const SI*);
virtual ~SafeIterator();
SI* operator->() throw (std::runtime_error);
SI* operator*() throw (std::runtime_error);
private:
SI* _mySafeIterator;
SafeIterator(const 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
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...
6
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...
4
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...
6
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...
6
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 =...
5
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, ...
2
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...
3
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...
27
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.