Help preventing weak software

B

Barry Hynes

Hi Folks,

could somebody point of any of the flaws in the following code.
I am mostly interested in trying to get the constructors to throw an error.

also in operator= not sure how or what to return if and error is detected.
inside catch(...) i return *this...not sure if this is correct

any help greatly appreciated

Barry

// safeList.h

#ifndef SAFELIST_H
#define SAFELIST_H

using namespace std;
#include <list>
#include <iterator>
#include <memory>
#include <stdexcept>


template<typename T>
class SafeList : private list<T> {
public:

using list<T>::push_front;
using list<T>::size;
using list<T>::insert;
using list<T>::begin;
using list<T>::end;
using list<T>::erase;
using list<T>::swap;
using list<T>::front;
using list<T>::back;
using list<T>::push_back;
using list<T>::pop_front;
using list<T>::pop_back;
using list<T>::splice;
using list<T>::remove;
using list<T>::unique;
using list<T>::merge;
using list<T>::reverse;
using list<T>::sort;
//constructors
SafeList() try : list<T>(){} catch(...) { cout << "Error in SafeList
No-arg Ctor." << endl;}//No no-arg ctor

SafeList(const T* first, const T* last) try : list<T>(){
insert(begin(), *first, *last);
}
catch(...){
cout << "Error in SafeList(const T* first, const T* last) " << endl;
}

SafeList(const SafeList<T>& x) try : list<T>(){
insert(begin(), x.begin(), x.end());
}
catch(...){
cout << "Error in SafeList(const SafeList<T>&) " << endl;
}

//destructor
~SafeList(){
erase(begin(), end());
}

//assignment
SafeList& operator=(const SafeList<T>& x) try{
if(this != &x){
typename list<T>::iterator f1 = begin();
typename list<T>::iterator l1 = end();
typename list<T>::const_iterator f2 = x.begin();
typename list<T>::const_iterator l2 = x.end();
while(f1 != l1 && f2 != l2)
*f1++ = *f2++;
if (f2 == l2)
erase(f1, l1);
else
insert(l1, f2, l2);
}
return *this;
}
catch(...){
cout << "Error in SafeList operator= " << endl;
return *this; ////////I PROBABLY DO NOT WANT TO DO THIS!!!!!!!!!!!not
sure how to get around it ;(
}

void print(){
typename list<T>::iterator xx;
for(xx = begin(); xx != end(); xx++)
cout << *xx << endl;
}

private:
int _size;
};

template <typename T>
inline bool operator==(const SafeList<T>& x, const SafeList<T>& y) {
return x.size() == y.size() && equal(x.begin(), x.end(), y.begin());
}

template <typename T>
inline bool operator<(const SafeList<T>& x, const SafeList<T>& y) {
return lexicographical_compare(x.begin(), x.end(), y.begin(), y.end());
}

#endif

//***********************

#include <iostream>
#include "safeList.h"

using namespace std;

int main()
{
try{
SafeList<int> SL;
SL.push_front(10);
SL.push_front(20);
SL.push_front(30);
SL.push_front(40);
int x = 5;
int y = 0;
int* a;
int* b;

a = &x;
b = &y;
cout << "The size of the list is: " << SL.size() << endl;
SafeList<int> SL2(a,b);
cout << "The size of the list is: " << SL2.size() << endl;

SafeList<int> SL3(SL2);
SL3.push_front(100);
SL3.push_front(200);
cout << "The size of the list is: " << SL3.size() << endl;
SafeList<int> SL4;

SL4 = SL3;
SL4.push_front(300);
cout << "The size of the list is: " << SL4.size() << endl;
cout << (SL4 == SL3) << " " << (SL4 < SL3) << " " << (SL3 < SL4) <<
endl;
for (long a = 0; a <= 1000 ; a++ )
{
SL.push_front(a);
cout << a << endl;
}
SL2 = SL;
for (long a = 0; a <= 1000 ; a++ )
{
SL2.push_front(a);
cout << (1000 - a) << endl;
}
cout << "The size of the list is: " << SL.size() << endl;
cout << "The size of the list is: " << SL2.size() << endl;
SL2.sort();
SL2.print();
cout << endl;
SL.print();
cout << endl;
SL3.print();
cout << endl;
SL4.print();
}
catch(...){
cout << " Error in main " << endl;
}
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,881
Members
47,418
Latest member
NoellaXku

Latest Threads

Top