In an object, I have an array of const-handles to some object of another specific class. In a method, I may want to return one of this handles as an inout
-parameter. Here as a simplified example:
class A {}
class B {
const(A) a[];
this() {
a = [new A(), new A(), new A()];
}
void assign_const(const(A)* value) const {
// *value = a[0]; // fails with: Error: cannot modify const expression *value
}
}
void main() {
const(A) a;
B b = new B();
b.assign_const(&a);
assert(a == b.a[0]); // fails .. obviously
}
I do not want to remove the const in the original array. Class B
is meant as some kind of view onto a collection constant A
-items. I'm new to D coming from C++. Do I have messed up with const-correctness in the D-way? I've tried several ways to get this to work but have no clue how to get it right.
How is the correct way to perform this lookup without "evil" casting?