Search code examples
c++compiler-constructionconstantsconst-iterator

Does a const instantiation of a custom class make *everything* in the class constant too?


  1. How do most compilers actually deal with const? What does it mean technically? (not practically)

When I say everything I mean, suppose I have a custom class with a container as a member field, and then a function that passes by reference values and appends them into that member field.

  1. If I have a const instantiation of this class, will constantness filter down?

  2. If I have a const STL iterator of a non const container (of my sharedPointers to instantiations of my custom class), what exactly is const about it? Will I be able to alter the contents of the container using the aforementioned function? Will the pointed to data be mutable but nothing else?


Solution

    1. Off topic for this site.

    2. If I have a const instantiation of this class, will constantness filter down? Yes.

    3. If I have a const STL iterator of a non const container. If const iterator then you cannot increment or decrement the iterator, but you can use it to modify its referent. If const_iterator then you can increment or decrement it, but you cannot use it to modify its referent. In either case, other non-const iterators to the container may also exist, so the container may be modified by them.