overload of array operations

Jonathan M Davis jmdavisProg at gmx.com
Fri Oct 14 16:56:40 PDT 2011


On Friday, October 14, 2011 16:12 Jay Norwood wrote:
> Jonathan M Davis Wrote:
> > On Friday, October 14, 2011 15:29:17 Jay Norwood wrote:
> > > Jonathan M Davis Wrote:
> > > > On Friday, October 14, 2011 11:30:25 Jay Norwood wrote:
> > > > > Is it possible to overload array operations
> > > > 
> > > > Please be more specific. Are you asking whether a struct or class can
> > > > overload the indexing and slicing operators? If so, the answer is
> > > > yes.
> > > > 
> > > > http://d-programming-language.org/operatoroverloading.html
> > > > 
> > > > - Jonathan M Davis
> > > 
> > > to be more specific, I'm interested in overloading the vector
> > > operations on arrays described at this link, search for "vector
> > > operation"
> > > 
> > > http://www.digitalmars.com/d/2.0/arrays.html
> > 
> > You could probably do it if you're fancy, but there's no explicit way to
> > have a struct or class operate like that. If you really wanted to
> > though, you could overload opSlice on your struct to return a specific
> > type which then overloaded opBinary for + and then have that return a
> > new struct with the changed values. But that borders on overloaded
> > operator abuse.
> > 
> > Vector operations are really only intended for arrays.
> > 
> > - Jonathan M Davis
> 
> Yes, I intended to try to overload the array operations on an array of
> structures. I just want to use the simple syntax for the operations.
> 
> a[]= b[]+c[];
> a[] = b[] + 4;
> a[] *= 4;
> 
> where a, b, c are arrays of structures, and then overload the operations. 
> It seems to me that should require names for the array operations being
> overloaded.

So, you want to have use vector operations on arrays of structs rather than 
use vector operations on a struct or class? If that's what you want, then 
overload the arithmetic operations on the struct as normal, and then use 
vector operations on the array holding the structs. It may work. I don't know. 
If it doesn't, then you're out of luck and can't use the built in syntax. And 
if does, well then there you go.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list