Thread overview |
---|
January 02, 2008 Classes with indexes | ||||
---|---|---|---|---|
| ||||
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. |
January 03, 2008 Re: Classes with indexes | ||||
---|---|---|---|---|
| ||||
Posted in reply to peter | > 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 |
January 03, 2008 Re: Classes with indexes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel919 | 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.
|
Copyright © 1999-2021 by the D Language Foundation