Tuesday, 15 September 2015

pointers - inout-parameter - replace one const-handle with another -



pointers - inout-parameter - replace one const-handle with another -

in object, have array of const-handles object of specific class. in method, may want homecoming 1 of handles inout-parameter. here simplified example:

class {} class b { const(a) a[]; this() { = [new a(), new a(), new a()]; } void assign_const(const(a)* value) const { // *value = a[0]; // fails with: error: cannot modify const look *value } } void main() { const(a) a; b b = new b(); b.assign_const(&a); assert(a == b.a[0]); // fails .. }

i not want remove const in original array. class b meant kind of view onto collection constant a-items. i'm new d coming c++. have messed const-correctness in d-way? i've tried several ways work have no clue how right.

how right way perform lookup without "evil" casting?

casting away const , modifying element undefined behavior in d. don't it. 1 time const, it's const. if element of array const, can't changed. so, if have const(a)[], can append elements array (since it's elements const, not array itself), can't alter of elements in array. it's same immutable. instance, string alias immutable(char)[], why can append string, can't alter of elements.

if want array of const objects can alter elements in array, need level of indirection. in case of structs, utilize pointer:

const(s)*[] arr;

but won't work classes, because if c class, c* points reference class object, not object itself. classes, need do

rebindable!(const c) arr;

rebindable in std.typecons.

pointers casting d const-correctness const-cast

No comments:

Post a Comment