In C++ we have the following:
int* p1; // pointer to int
const int* p2; // pointer to constant int
int* const p3; // constant pointer to int
const int* const p4; // constant pointer to constant int
and in D:
int* p1; // pointer to int
const(int)* p2; // pointer to constant int
?? ?? ?? // constant pointer to int
const(int*) p4; // constant pointer to constant int
what's the syntax for constant pointer to int
?
D does not have head const.
I think you can simulate it:
struct Ptr(T)
{
T* _val;
this(T* nval) const
{
_val = nval;
}
@property T* opCall() const
{
return cast(T*)_val;
}
alias opCall this;
}
void main()
{
int x = 1;
int y = 2;
const Ptr!int ptrInt = &x;
assert(*ptrInt == 1);
*ptrInt = y; // ok
assert(*ptrInt == 2);
assert(x == 2);
ptrInt = &y; // won't compile, good.
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With