Constructor returns wrong size of stl::list<T>??

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>::push_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;
}
 
C

Chris Jefferson

Barry said:
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>::push_front;
using list<T>::size;
using list<T>::insert;
using list<T>::begin;
//assignment
// SafeList<T>& operator=(const SafeList<T>&);
private:
int _size;
};

It would be more useful if you told us a) what you get and b) what you
expect. You are calling the size function from list, so exactly what you
set _size to I would expect to make no difference at all, as
list<T>::size is not going to either see it or have any interest in it.

Chris
 
E

Eric Lilja

Barry Hynes said:
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>::push_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;
}

The provided code wasn't minimal. Your problem is insert(pos, n, elem);
inserts at iterator position pos n copies of elem. n in your case is 1. So
it inserts one copy of the element 10. Thus the size of the list is 1.

/ Eric
 
B

Barry Hynes

Chris Jefferson said:
It would be more useful if you told us a) what you get and b) what you
expect. You are calling the size function from list, so exactly what you
set _size to I would expect to make no difference at all, as
list<T>::size is not going to either see it or have any interest in it.

i expect 10 but i get 1...it is included in main.

i should not have to include _size in SafeList...i would like to use size()
from list<T> but i
cannot get the correct value. ;(

thanks
Barry
 
B

Barry Hynes

thank you...

owe u a beer...

thanks man

Barry

Eric Lilja said:
The provided code wasn't minimal. Your problem is insert(pos, n, elem);
inserts at iterator position pos n copies of elem. n in your case is 1. So
it inserts one copy of the element 10. Thus the size of the list is 1.

/ Eric
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top