The Status of Const
Steven Schveighoffer
schveiguy at yahoo.com
Fri Aug 13 06:35:13 PDT 2010
On Thu, 12 Aug 2010 21:45:16 -0400, Jonathan M Davis
<jmdavisprog at gmail.com> wrote:
> 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.
This is not a good idea. The type system treats class references
specially. Any attempt to use a pointer to reference the actual data will
most certainly end up being untyped and pretty much useless.
-Steve
More information about the Digitalmars-d
mailing list