The Status of Const

Jonathan M Davis jmdavisprog at gmail.com
Thu Aug 12 18:45:16 PDT 2010


On Thursday, August 12, 2010 18:16:01 Graham St Jack wrote:
> On 13/08/10 10:18, Jonathan M Davis wrote:
> > On Thursday, August 12, 2010 17:38:28 Graham St Jack wrote:
> >> For me, the key problem is that a class object reference has the same
> >> const/immutable/shared attribute as the object on the heap that it
> >> refers to. This is sometimes what you need, but more often you want a
> >> non-shared, mutable reference to a const/immutable/shared object.
> >> 
> >> You can achieve this with pointers for arrays, structs and primitive
> >> types, but not with classes because a class pointer is just a pointer to
> >> a reference.
> > 
> > Hence the hack that is Rebindable!().
> > 
> > Oh, and you _can_ achieve pointers to classes, but what you normally use
> > are references, which do have the problem of not being able to be split
> > between the reference and referent types.
> > 
> > - Jonathan M Davis
> 
> So how do you get a pointer to an object? Taking the address of an
> object reference gives you a pointer to the reference, not the object,
> which is fair enough. As far as I know there isn't a way to get a
> pointer to the object itself, and even if you could, how do you use such
> a thing?

Hmm. I was thinking that you could just do

T* t = new T();

but that doesn't work. I know that people have talked about doing it here on the 
newsgroup, so there must be a way. You can do

T t1 = new T();
T* t2 = &t1;

but I guess that that's a pointer to a reference rather a pointer to the object 
itself. Maybe if you want pointers to classes you need to use manual memory 
manegement rather than the GC and new. Hopefully someone else can enlighten us. 
I have generally avoided pointers in D.

- Jonathan M Davis


More information about the Digitalmars-d mailing list