Classes with indexes

peter p.adject at mdt.org
Wed Jan 2 20:21:56 PST 2008


Daniel919 Wrote:

> > I have the following class...
> > 
> > class myclass
> > {
> >     private ubyte[] Array;
> > }
> > 
> > ...and i want to create an instance of this class and access the array as follows:
> > 
> > myclass mc = new myclass();
> > mc[0] = 100;
> 
> import std.stdio;
> 
> class myclass
> {
> 	private ubyte[] Array;
> 	
> 	void opIndexAssign(ubyte value, size_t i) {
> 		if( Array.length <= i )
> 			Array.length = i+1;
> 		Array[i] = value;
> 	}
> 	
> 	ubyte opIndex(size_t i) { return Array[i]; }
> }
> 
> void main()
> {
> 	myclass mc = new myclass();
> 	mc[0] = 100;
> 	writefln( mc[0] );
> }
> 
> 
> 
> http://www.digitalmars.com/d/operatoroverloading.html
> 
> Best regards,
> Daniel

Hahaha, cool man thanks, thatīs exactly what i needed.

"Array Operator Overloading: Overloading Indexing" Thatīs the name for it. Iīve learned something new today, thanks again.


More information about the Digitalmars-d-learn mailing list