Custom template iterator

M

maverik

Hi all.

I'm implementing class AbstractCollection - just a wrap on std::vector
- to hide from clients the fact of using std::vector in the base
(because in the future I may change vector to a list or another DS).
So I try to do it in this way:

template<typename T>
class AbstractCollection {
public:
AbstractCollection() : m_collection() {};
AbstractCollection(const AbstractCollection &collection);
AbstractCollection(const int n) : m_collection(n) {};

typedef std::vector<T>::iterator iterator; //
Try to define iterators for AbstractCollection
typedef std::vector<T>::const_iterator const_iterator;
private:
std::vector<T> m_collection;
};

But the complier complains:

error C2146: syntax error : missing ';' before identifier 'iterator'
....

Can you tell me how to do it in the right way (how can I define
iterators for AbstractCollection using std::vector iterators)?
It would be nice if you also tell me what's wrong with my method (or
provide link to read about).

Thanks.
 
A

anon

maverik said:
Hi all.

I'm implementing class AbstractCollection - just a wrap on std::vector
- to hide from clients the fact of using std::vector in the base
(because in the future I may change vector to a list or another DS).
So I try to do it in this way:

template<typename T>
class AbstractCollection {
public:
AbstractCollection() : m_collection() {};
AbstractCollection(const AbstractCollection &collection);
AbstractCollection(const int n) : m_collection(n) {};

typedef std::vector<T>::iterator iterator; //
Try to define iterators for AbstractCollection
typedef std::vector<T>::const_iterator const_iterator;

The correct way is:
typedef typename std::vector said:
private:
std::vector<T> m_collection;
};

But the complier complains:

error C2146: syntax error : missing ';' before identifier 'iterator'
...

Can you tell me how to do it in the right way (how can I define
iterators for AbstractCollection using std::vector iterators)?
It would be nice if you also tell me what's wrong with my method (or
provide link to read about).

Search for "template dependent name"
 
S

Salt_Peter

Hi all.

I'm implementing class AbstractCollection - just a wrap on std::vector
- to hide from clients the fact of using std::vector in the base
(because in the future I may change vector to a list or another DS).

Exactly, that should give you a clue why you get the error.
A list and a vector do not have the same type of iterator.
So I try to do it in this way:

template<typename T>
class AbstractCollection {
public:
AbstractCollection() : m_collection() {};
AbstractCollection(const AbstractCollection &collection);
AbstractCollection(const int n) : m_collection(n) {};

AbstractCollection( const std::size_t n, const T& t)
: m_collection(n, t) {};


typedef std::vector<T>::iterator iterator;

Since std::vector<T>::iterator is a dependent type:

typedef typename std::vector<T>::iterator iterator;

iterator begin() { return m_collection.begin(); }

and so on...
typedef std::vector<T>::const_iterator const_iterator;
private:
std::vector<T> m_collection;

};

But the complier complains:

error C2146: syntax error : missing ';' before identifier 'iterator'
...

Can you tell me how to do it in the right way (how can I define
iterators for AbstractCollection using std::vector iterators)?
It would be nice if you also tell me what's wrong with my method (or
provide link to read about).

Nothing wrong in encapsulating a std::vector,
you are doing it exactly as planned.
The only issue is the name AbstractCollection since its not abstract?
How about Vector, Container or Collection.
 
M

maverik

Thank you guys.
Nothing wrong in encapsulating a std::vector,
you are doing it exactly as planned.
The only issue is the name AbstractCollection since its not abstract?
How about Vector, Container or Collection.

Thanks, you are right.
 
G

Gennaro Prota

maverik said:
Thank you guys.


Thanks, you are right.

That's not the only issue. Check Item 2 in "Effective STL" by
Scott Meyers:

Beware the illusion of container-independent code
 
J

James Kanze

That's not the only issue. Check Item 2 in "Effective STL" by
Scott Meyers:
   Beware the illusion of container-independent code

Exactly. As long as the iterator is just a typedef, he's not
encapsulating anything. To effectively encapsulate, he also has
to encapsulate the iterator.
 

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

Latest Threads

Top