const and immutable member variables in classes
anonymous via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Feb 2 15:02:28 PST 2016
On 02.02.2016 23:48, Ali Çehreli wrote:
> struct S {
> const int c; // Makes S non-assignable
> immutable int i; // Makes S non-assignable
> }
>
> void main() {
> auto a = S();
> auto b = S();
> a = b; // Compilation ERROR
> }
>
> (That is the same issue in C++.)
>
> That's why I've been avoiding them altogether. However, considering that
> there is no default-assignment for classes, there is no problem with
> using const or immutable members with classes, right?
I'm not sure what you mean by "default assignment". I'd say it works
more simply with classes, because they're reference types. It's the same
as using pointers to structs:
auto a = new S();
auto b = new S();
a = b; /* no problem */
More information about the Digitalmars-d-learn
mailing list