I have a class which I intend for others to inherit from. It has a std::vector which I only want developers to be able to read from, but not modify, my base functions modify it. Should I provide a function that returns a const iterator, or expose the vector as protected.
Thanks
If you expose the vector as protected, subclasses will be able to modify it. So, you should expose methods that return const iterators.
You can use the Non-Virtual Interface idiom to expose different interfaces for users and subclasses.
If you protected it, then you lose your protection because any subclass can change it to public and let others to modify it.
Why not provide a const reference? If return const iterator, you may need to rewrite a lot interfaces including begin, end, size etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With