B
Barry Hynes
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
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