std::list<T>::iterator

I

Isliguezze

Does anybody know how to make a wrapper for that iterator? Here's my
wrapper class for std::list:

template <class T> class List {
private:
std::list<T> *lst;
public:
List() { lst = new std::list<T>(); }
List(const List<T> &rhs) { lst = new std::list<T>(*rhs.lst); }
List(int n, const T& value) { lst = new std::list<T>(n, value); }
~List() { delete lst; }

void push_back(const T& value) { lst->push_back(value); }
void push_front(const T& value) { lst->push_front(value); }
void pop_back() { lst->pop_back; }
void pop_front() { lst->pop_front; }
void remove(const T& value) { lst->remove(value); }
};
 
I

Isliguezze

What is the purpose of this class?
Good question. It is kind of "I want to understand STL a little bit
better". You know, I have to implement a "list" anyhow. So I decided
write it using STL. Also, as I told before, I want to know how all
this is cooked.
 
I

Isliguezze

Frankly speaking, I have to add std::list::insert function to the
wrapper and I don't know how to do it without iterators. Also, I want
to have own iterators to let user to iterate (I mean to do for
(List<T>::Itr i = L.begin(); ... etc)
 
V

Vincent Jacques

Isliguezze a écrit :
Does anybody know how to make a wrapper for that iterator? Here's my
wrapper class for std::list:

(I assume that you don't wrap just for the sake of wrapping, and that
the added value of your wrapper is hiden for the purpose of your question.)
template <class T> class List {
private:
std::list<T> *lst;
public:
List() { lst = new std::list<T>(); }
List(const List<T> &rhs) { lst = new std::list<T>(*rhs.lst); }
List(int n, const T& value) { lst = new std::list<T>(n, value); }
~List() { delete lst; }

void push_back(const T& value) { lst->push_back(value); }
void push_front(const T& value) { lst->push_front(value); }
void pop_back() { lst->pop_back; }
void pop_front() { lst->pop_front; }

These two line most probably don't do what you think: you don't call
void remove(const T& value) { lst->remove(value); }
};

Well, for the iterator, you could add a class Iterator { ... }; in the
definition of your List.

With the same level of added value, this Iterator class would have a
member data named itr, and you would implement a forwarding function for
each function of std::list<T>::iterator, as you did for list.

I hope it helps,
 
V

Vincent Jacques

Isliguezze a écrit :
You know, I have to implement a "list" anyhow.

No offense, but you're not *implementing* a list. You're adding a layer
on top of the existing (and well implemented) std::list.

I mean, if you are implementing a list as an exercise, it would be much
better to really implement it, with the double-linked cells (the teacher
will not be happy with the forwarding version, and you will have learned
only a few things)

I you want to *use* you List, why not use std::list directly?

Cheers,
 
I

Isliguezze

Actually I didn't really understand how to make own version of the
iterator... make a new class Iterator and use it as a friend classof
what? How to do so that a user could just write something like this:
List<T> lst;
for (List<T>::It i = lst.begin(); i != lst.end(); ++i) {
//...
}
?
 
I

Isliguezze

But gentlemen, if I do not use pointers, how do I make the destructor?
Is this correct: ~List() { rep.~list(); } ?
 
I

Isliguezze

But gentlemen, if I do not use pointers, how do I make the destructor?
Is this correct: ~List() { rep.~list(); } ?
 
I

Isliguezze

But gentlemen, if I do not use pointers, how do I make the destructor?
Is this correct: ~List() { rep.~list(); } ?
 
F

Frank Birbacher

Daniel said:
No. The correct d_tor would be: ~List() { }

Following functionality would automatically be correct, meaning
"out-of-the-box" ready:

- Copy Constructor
- Assignment operator
- Destructor

That means you write no code, but have full functionality!!

This standard behaviour works like these functions:

template<typename T>
class List
{
std::list<T> lst;
public:
List(List<T> const& original)
: lst(original.lst)
{}

List<T>& operator = (List<T> const& original)
{
lst = original.lst;
return *this;
}

~List() {}
};

But you don't even need to write them!!

Regards, Frank
 
J

Jerry Coffin

But gentlemen, if I do not use pointers, how do I make the destructor?
Is this correct: ~List() { rep.~list(); } ?

No. The dtor you included in your original post matched up correctly
with the ctors you included.

You typically only use the direct dtor call syntax like above in
conjunction with a placement new operator.
 
R

Roland Pibinger

Actually I didn't really understand how to make own version of the
iterator... make a new class Iterator and use it as a friend classof
what? How to do so that a user could just write something like this:
List<T> lst;
for (List<T>::It i = lst.begin(); i != lst.end(); ++i) {
//...
}

See: http://cpphelp.com/slist/
 

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
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top