opStar

Steven Schveighoffer schveiguy at yahoo.com
Sat Nov 10 06:12:00 PST 2007


"Janice Caron" wrote
> So, we can write p.m for real pointers, but we have to write (*p).m
> for classes that implement opStar()?

You can also write (*p).m for pointers :P

> That seems inconsistent.

I think it has to be.  If we examine C++, the normal pointer operator -> has 
no real meaning by default for a class.  This allows them to define the 
operator without possibility of collision.

In D, the dot DOES have a meaning, and so you would be overriding that 
meaning depending on the member variable.  To illustrate the point, what if 
you have a pointer class template, somone uses it, but happens to point to a 
class that has a member named the same as one of the pointer class' members? 
Now your pointer class has no value for that specific class type.

> What's opStar for, if not to implement iterators?

You can still implement iterators, even without opStar.

With opStar, if you consistently use pointers in the (*p).m fashion, a 
pointer class can be swapped in.  Yeah, it's ugly, but possible :)  If you 
are writing a template function that should take either pointers or 
pointer-like classes, then write the (*p).m form, and it should work.

-Steve 





More information about the Digitalmars-d mailing list