Extending stl::list???

Discussion in 'C++' started by Barry Hynes, Feb 11, 2005.

  1. Barry Hynes

    Barry Hynes Guest

    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
    Barry Hynes, Feb 11, 2005
    #1
    1. Advertising

  2. Barry Hynes

    David Hilsee Guest

    "Barry Hynes" <-mpo.gc.ca> wrote in message
    news:uB5Pd.230465$...
    > 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
    David Hilsee, Feb 12, 2005
    #2
    1. Advertising

  3. Barry Hynes

    barman Guest

    "David Hilsee" <> wrote in message
    news:D...
    > "Barry Hynes" <-mpo.gc.ca> wrote in message
    > news:uB5Pd.230465$...
    >> 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
    barman, Feb 13, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian O'Brien

    Need help extending stl list.

    Brian O'Brien, May 28, 2004, in forum: C++
    Replies:
    0
    Views:
    313
    Brian O'Brien
    May 28, 2004
  2. JustSomeGuy

    Need help extending stl list.

    JustSomeGuy, May 28, 2004, in forum: C++
    Replies:
    6
    Views:
    369
    JustSomeGuy
    Jun 1, 2004
  3. Alan
    Replies:
    4
    Views:
    341
    Victor Bazarov
    Jun 25, 2005
  4. Replies:
    5
    Views:
    493
    Markus Schoder
    Apr 16, 2006
  5. Replies:
    2
    Views:
    379
Loading...

Share This Page