On Sunday, 30 September 2018 at 09:16:42 UTC, Nicholas Wilson
wrote:
On Sunday, 30 September 2018 at 07:29:00 UTC, Vijay Nayar wrote:
Second question. const class variables may not be re-assigned,
so if you need a variable that may be reassigned, but may
never modify the underlying object, a const pointer can be
useful. However, it seems that when gets the address of a
class variable, you do not get the underlying address of the
class object.
&a = 0x7ffd0800acb8, a = 0x7fd6b05b0000, a.data=4
&a = 0x7ffd0800acd0, a = 0x7fd6b05b0000, a.data=4
The stack ^ the heap^ data on the heap^
The address of the variable a on the stack has different values
across function calls, its value (the reference to the class
data) remains the same, as does the data itself.
Is there a way to either have a constant reference to a class
that can be set to a new value, or is there a way to convert the
class variable to a class pointer?
For example:
void main()
{
class Thing {}
class ThingSaver {
// A const(Thing) could not be changed in
setThing().
const(Thing)* t;
void setThing(in Thing thing) {
t = thing; // ERROR converting to pointer type!
}
const(Thing) getThing() const {
return *t;
}
}
Thing t1 = new Thing();
ThingSaver saver = new ThingSaver();
saver.setThing(t1);
const(Thing) t2 = saver.getThing();
}