Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Extending stl::list???

Reply
Thread Tools

Extending stl::list???

 
 
Barry Hynes
Guest
Posts: n/a
 
      02-11-2005
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



 
Reply With Quote
 
 
 
 
David Hilsee
Guest
Posts: n/a
 
      02-12-2005
"Barry Hynes" <(E-Mail Removed)-mpo.gc.ca> wrote in message
news:uB5Pd.230465$(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
barman
Guest
Posts: n/a
 
      02-13-2005

"David Hilsee" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> "Barry Hynes" <(E-Mail Removed)-mpo.gc.ca> wrote in message
> news:uB5Pd.230465$(E-Mail Removed)...
>> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Extending the reception of a network yar Wireless Networking 3 06-15-2005 05:16 PM
extending a wireless Stuart Turrell Wireless Networking 6 11-30-2004 10:18 PM
Extending a Vlan Dak991 Cisco 1 11-23-2004 06:48 PM
Extending console cable Stibloc Cisco 3 02-29-2004 02:34 AM
Extending a T1 over Ethernet / Fiber link Rick G Cisco 2 07-11-2003 09:22 PM



Advertisments