Re: How to print container elements which are pointers
On Thursday, November 8, 2012 8:36:30 PM UTC-6, V.Subramanian, India wrote:
c.officers.push_back(new Person(string("one")));
To expose a pointer-based interface is very dangerous, since it's too
easy to leak a new-ed memory (your sample program already leaked) or
encounter a data corruption if you insist to delete the pointers
somewhere.
Anyway, here is my suggestion: to define a custom iterator.
First, as usual, you need an output operator for Person:
friend ostream& operator<<(ostream& out, Person const& p) {
out << p.name;
return out;
}
And then, a proxy-like iterator in Club:
class Club {
public:
struct iterator : list<Person>::iterator {
explicit iterator(list<Person*>::iterator i) : ptr_(i) {}
iterator(iterator const& i) : ptr_(i.ptr_) {}
iterator& operator=(iterator i) {
using std::swap;
swap(this->ptr_, i.ptr_);
return *this;
}
reference operator*() const {
return **ptr_;
}
pointer operator->() const {
return &(operator*());
}
iterator& operator++() {
++ptr_;
return *this;
}
iterator operator++(int) {
iterator tmp(*this);
++(*this);
return tmp;
}
/* operator-- omitted */
friend bool operator==(iterator x, iterator y) {
return x.ptr_ == y.ptr_;
}
friend bool operator!=(iterator x, iterator y) {
return !(x == y);
}
private:
list<Person*>::iterator ptr_;
};
list<Person*> officers;
iterator begin() {
return iterator(officers.begin());
}
iterator end() {
return iterator(officers.end());
}
};
So that you can use
copy(c.begin(),
c.end(),
ostream_iterator<Person>(cout, "\n"));
To print the Club. Cool?