www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Classes with indexes

reply peter <p.adject mdt.org> writes:
Hello guys,

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;

I was complety sure i saw this in the newsgroup but i canīt remember where
exactly.
How is this "thing" called?
In the .NET framework i see it in almost every library.

I hope you guys get the idea, please help.
Jan 02 2008
parent reply Daniel919 <Daniel919 web.de> writes:
 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
Jan 02 2008
parent peter <p.adject mdt.org> writes:
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.
Jan 02 2008