is it possible to define a property to access (read/write) a matrix? (I.e., more dimensions than a vector)

Stewart Gordon smjg_1998 at yahoo.com
Sun Jul 30 10:07:24 PDT 2006


Charles D Hixson wrote:
<snip>
> Although... well, I was asking about something that would
> look more like:
> 
> class MyMatrix
> {
>    float at(uint i, uint j)
>    {
>       return values[i][j];
>    }
> 
>    float at(float val, uint i, uint j)
>    {
>       values[i][j] = val;
>       return val;  //  Not sure about this line

What is there to be not sure about about it?  Whether you can shorten 
the two statements to

     return values[i][j] = val;

?  Well, of course you can.

>    }
> }
> 
> MyMatrix mat = new mat;
> mat.at(0,0) = 0.0f;
> float f = mat.at(0,0);
> 
> where the property is "at".  Knowing this would still be
> useful, as I might some time want to access things via more
> than one method (imagine I had sorted indexes, and was
> retrieving the same thing sometimes by name and sometimes by
> date, and occasionally by record number).
> Still, if this works for opIndexAssign, it probably works
> for all properties.

You could define a property that returns a wrapper object, and then use 
opIndex(assign) on that.  For example:

     class MyMatrix {
        struct At {    // I don't think there are "inner" structs as such
             MyMatrix m;

             float opIndex(uint i, uint j) {
                 return m.values[i][j];
             }

             float opIndexAssign(float val, uint i, uint j) {
                 return m.values[i][j] = val;
             }
         }

         At at() {
             return * cast(At*) cast(void*) &this;
         }
     }

     MyMatrix mat = new mat;
     mat.at[0,0] = 0.0f;
     float f = mat.at[0,0];

Except that values is undeclared....

Stewart.

-- 
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS/M d- s:-@ C++@ a->--- UB@ P+ L E@ W++@ N+++ o K-@ w++@ O? M V? PS- 
PE- Y? PGP- t- 5? X? R b DI? D G e++++ h-- r-- !y
------END GEEK CODE BLOCK------

My e-mail is valid but not my primary mailbox.  Please keep replies on 
the 'group where everyone may benefit.



More information about the Digitalmars-d-learn mailing list