By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,419 Members | 1,124 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,419 IT Pros & Developers. It's quick & easy.

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

P: n/a
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;
}
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Barry Hynes wrote:
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
Jul 23 '05 #2

P: n/a

"Barry Hynes" <hy****@mar.dfo-mpo.gc.ca> wrote in message
news:B2*******************@ursa-nb00s0.nbnet.nb.ca...
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
Jul 23 '05 #3

P: n/a

"Chris Jefferson" <ca*@cs.york.ac.uk> wrote in message
news:42**************@cs.york.ac.uk...
Barry Hynes wrote:
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.


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
Chris

Jul 23 '05 #4

P: n/a
thank you...

owe u a beer...

thanks man

Barry

"Eric Lilja" <er*************@yahoo.com> wrote in message
news:cv**********@news.island.liu.se...

"Barry Hynes" <hy****@mar.dfo-mpo.gc.ca> wrote in message
news:B2*******************@ursa-nb00s0.nbnet.nb.ca...
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

Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.