B
Barry Hynes
Hi folks,
still foolin with SafeList...
anyhow why does the following code return the wrong size for SL2
any help greatly appreciated
Thanks:
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:
SafeList() : list<T>(){_size = 0;}
SafeList(const T* first, const T* last) : list<T>(){
_size = 0;
insert(begin(), *first, *last);
_size = (*last - *first) + 1 ;
cout << "My way of getting _size: " << _size << "..... exiting ctor"
<<endl;
}
// SafeList(const SafeList<T>&);
//destructor
~SafeList(){}
using list<T>:ush_front;
using list<T>::size;
using list<T>::insert;
using list<T>::begin;
//assignment
// SafeList<T>& operator=(const SafeList<T>&);
private:
int _size;
};
#endif
//***************************************************
#include <iostream>
#include "safeList.h"
using namespace std;
int main()
{
SafeList<int> SL;
SL.push_front(10);
SL.push_front(20);
SL.push_front(30);
SL.push_front(40);
int x = 1;
int y = 10;
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;
cout << "this should be 10 not 1" <<endl;
}
still foolin with SafeList...
anyhow why does the following code return the wrong size for SL2
any help greatly appreciated
Thanks:
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:
SafeList() : list<T>(){_size = 0;}
SafeList(const T* first, const T* last) : list<T>(){
_size = 0;
insert(begin(), *first, *last);
_size = (*last - *first) + 1 ;
cout << "My way of getting _size: " << _size << "..... exiting ctor"
<<endl;
}
// SafeList(const SafeList<T>&);
//destructor
~SafeList(){}
using list<T>:ush_front;
using list<T>::size;
using list<T>::insert;
using list<T>::begin;
//assignment
// SafeList<T>& operator=(const SafeList<T>&);
private:
int _size;
};
#endif
//***************************************************
#include <iostream>
#include "safeList.h"
using namespace std;
int main()
{
SafeList<int> SL;
SL.push_front(10);
SL.push_front(20);
SL.push_front(30);
SL.push_front(40);
int x = 1;
int y = 10;
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;
cout << "this should be 10 not 1" <<endl;
}