How to implement standard iterators in class

前端 未结 3 1559
温柔的废话
温柔的废话 2021-02-06 04:40

I have classes which are usually using standard containers as underlying fields. For example, I have a class

template 
class Vec_3D
{
public:
          


        
3条回答
  •  粉色の甜心
    2021-02-06 05:25

    std::iterator is a base class only, its basically a container for some traits, but if you wanted to use it to implement your own iterator class you'd need to derive from it.

    However you don't need to use it, there was a proposal to deprecate it, you could just define those traits directly in an iterator that you write. The following question has info on the proposal and help with implementing an iterator class:- Preparation for std::iterator Being Deprecated

    At the moment you're defining your container's iterator types using that base, not a class that can actually do any iterating, which is why it fails.

    You expose the array as a public member. If you're happy to expose that your vec_3d is implemented using an array (whether you continue to expose the member array publicly or not) then you could just use the array's iterators - its not clear from the question that your iterator needs any bespoke behaviour just because your container adds some functionality.

提交回复
热议问题