, const .
But it is still not "perfect" because you need to expose the container to the world, so if you change it, you will also change the user code if the interface is no longer the same.
But there is one last hope:
If you can use lambdas (C ++ 0x), you better provide the for_each algorithm:
class MyThingManager
{
public:
template< typename FunctorType >
void modify_each_thing( FunctorType f )
{
std::for_each( m_things.begin(), m_things.end(), f );
check_everything_is_still_valid();
notify_the_world();
}
template< typename FunctorType >
void for_each_thing( FunctorType f ) const { std::for_each( m_things.begin(), m_things.end(), f ); }
size_t things_count() const { return m_things;}
private:
std::vector<Thing> m_things;
};
Using:
MyThingManager manager;
manager.for_each_thing( []( const Thing& thing ){ std::cout << "\nA thing : " << thing; } );
Klaim source
share