Extended Type Design.
Tyler Knott
tywebmail at mailcity.com
Tue Mar 20 13:42:33 PDT 2007
Bruno Medeiros wrote:
>
> What?? If f is 'const', how are any of those "f.s =" assignments allowed?
>
Because f is a POD (Plain Old Data) structure and not a reference you can modify the data in the structure, but because
it's declared const you can't modify any data referenced. If you want f.s to be unassignable then declare f to be final
Foo, though you can then modify the data referenced by f.s so f.s[0] = 'a'; becomes legal.
For example:
struct Foo
{
char[] s = "hello";
char c;
}
const Foo const_foo;
final Foo final_foo;
final const Foo const_final_foo;
const Foo* const_foo_pointer = new Foo;
const_foo.s = "test"; //Legal: data directly modified (const only invalidates modification through references)
final_foo.s = "test"; //Illegal: cannot modify data in final variables
const_final_foo.s = "test"; //Illegal: cannot modify data in final variables
const_foo_pointer.s = "test"; //Illegal: cannot modify data through const references
const_foo.s[0] = 'a'; //Illegal: cannot modify data through const references
//(const declaration is transitive to const_foo.s reference)
final_foo.s[0] = 'a'; //Legal: you're not modifying any data in final_foo, only data referenced by it
const_final_foo.s[0] = 'a'; //Illegal: cannot modify data through const references
const_foo_pointer.s[0] = 'a'; //Illegal: cannot modify data through const references
More information about the Digitalmars-d
mailing list