472,353 Members | 2,200 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 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 2314
"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...
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...
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...
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...
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...
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...
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...
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...
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...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...

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.