Classes with indexes

Daniel919 Daniel919 at web.de
Wed Jan 2 16:06:46 PST 2008


> 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


More information about the Digitalmars-d-learn mailing list