Hiding class pointers -- was it a good idea?

Bill Baxter dnewsgroup at billbaxter.com
Wed Aug 15 10:42:59 PDT 2007


Reiner Pope wrote:
> Bill Baxter wrote:
> 
> How about allowing operator overloads on classes which must be treated 
> as reference types?
> 
> ByValue* a;
> // can't overload opAdd usefully:
> assert(a.opAdd(5) != a + 5);
> 
> ByRef b;
> // but we can if the pointer is hidden
> assert(b.opAdd(5) == a + 5);

That's a good point.  But it does kind of fall into the category of "no 
need to type '*' everywhere".  You could just say *a + 5.

Or in C++ you could use a reference: ByValue&.

But yeh, you pretty much need real reference types like in C++ if you 
don't hide the pointer.  Another alternative might be to make a 
distinction between traditional raw memory pointers and pointers to 
objects.  Then a+5 could automatically dereference 'a' if it's an object 
  pointer.  And if you want to do pointer arithmetic you would need some 
special syntax.  But now that I think about it, that 'object pointer' is 
really just a reference type by another name.  It's a pointer that acts 
like a value by automatically dereferencing when needed.

--bb



More information about the Digitalmars-d mailing list