PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Custom template iterator
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Custom template iterator

Réponse
 
LinkBack Outils de la discussion
Vieux 07/11/2008, 12h42   #1
maverik
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Custom template iterator

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.
  Réponse avec citation
Vieux 07/11/2008, 13h04   #2
anon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Custom template iterator

maverik wrote:
> 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<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).


Search for "template dependent name"
  Réponse avec citation
Vieux 07/11/2008, 22h13   #3
Salt_Peter
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Custom template iterator

On Nov 7, 6:42 am, maverik <maverik.m...@gmail.com> wrote:
> 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.

  Réponse avec citation
Vieux 08/11/2008, 12h33   #4
maverik
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Custom template iterator

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.
  Réponse avec citation
Vieux 08/11/2008, 14h17   #5
Gennaro Prota
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Custom template iterator

maverik wrote:
> 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.


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

Beware the illusion of container-independent code

--
Gennaro Prota | name.surname yahoo.com
Breeze C++ (preview): <https://sourceforge.net/projects/breeze/>
Do you need expertise in C++? I'm available.
  Réponse avec citation
Vieux 09/11/2008, 13h07   #6
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Custom template iterator

On Nov 8, 2:17pm, Gennaro Prota <gennaro/pr...@yahoo.com> wrote:
> maverik wrote:
> >> 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.


> 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.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 09h23.


Édité par : vBulletin®
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,79791 seconds with 14 queries