Boost logo

Boost Users :

Subject: Re: [Boost-users] [forEach] Working with custom operator []
From: Nathan Ridge (zeratul976_at_[hidden])
Date: 2011-05-08 12:28:36


> From: avishay.ben.shabtai_at_[hidden]
>
> Hi
> i have a container class that defines 2 overloaded [] operators:
> T& operator[](const chat *);
> T& operator[](unsigned int);
>
> when i am using BOOST_FOREACH(T val,container)
>
> i get compilation errors but working directly with iterators is working
> ok, any idea?

 
BOOST_FOREACH uses the begin() and end() member functions to get
iterators pointing to the beginning and end of the range,
then advances the begin iterator using ++ until it reaches the end.
Along the way it dereferences the iterator to get the the element
that is assigned to "val". It has nothing to do with operator[].
 
If you want your container to work with BOOST_FOREACH, you need
to make sure it has begin() and end() member functions which
return iterators with the appropriate capabilities (increment,
dereference, equality comparison).
 
If you're still having troubles, show us the compiler errors you get.
 
Regards,
Nate.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net