This is mostly an understanding check, as I couldn't find a complete reference on this topic.
In C#, when I write readonly Foo myFoo
, I'm essentially saying myFoo
is a pointer to Foo
, and the pointer cannot be reassigned. To guarantee that the underlying Foo
can't be reassigned, I need a whole other class or interface ImmutableFoo
.
Now consider the construct List<Foo>
. It's basically a pointer to a list of pointers to Foo
, i.e. similar to vector<Foo *> *
in C++. There are three places where you could put const
in C++.
const vector<const Foo *> * const
- const #1: You cannot modify the vector (by resizing, reassigning elements, etc.)
- const #2: You cannot modify the Foo-s pointed to inside the vector
- const #3: You cannot modify the pointer to the vector
So I think the equivalent of each of these is,
List<Foo> = vector<Foo *> * // No consts
ReadOnlyCollection<Foo> = const vector<Foo *> * // First const toggled
List<ImmutableFoo> = vector<const Foo *> * // Second const toggled
readonly List<Foo> = vector<Foo *> * const // Third const toggled
readonly ReadOnlyCollection<ImmutableFoo>
= const vector<const Foo *> * const // All consts toggled
Is this table of equivalencies correct?