Insert array into an AA

Robert Clipsham robert at octarineparrot.com
Tue Aug 16 12:52:54 PDT 2011


On 16/08/2011 20:17, nrgyzer wrote:
> Hi everyone,
>
> I've the following:
>
> private static ubyte[][2][hash_t] classInstances;
>
> this() {
>
>     classInstances[toHash()] = new ubyte[2]; // does not work
>
> }
>
> I want insert every class instance into the hashmap. Every class
> instance should be contained in the map after the constructor was
> called. When I compile my code, I get "Error: cannot implicitly
> convert expression (new ubyte[](2u)) of type ubyte[] to ubyte[][]"
> which is logical. But is there any way to insert every instance into
> the array and define the array/map for this entry?
>
> Thanks in advance!

Is there any particular reason you're using ubyte[][2][hash_t] there? To 
keep a reference to each instance simply use:
----
class MyClass {
     static MyClass[] classInstances;
     this() {
         classInstances ~= this;
     }
}
----

If you in fact want to have a hashmap indexed by the instances with 
values of type ubyte[][2], you can do this:
----
class MyClass {
     static ubyte[][2][MyClass] classInstances;
     this() {
         classInstances[this] = new ubyte[][2];
     }
}
----

The problem in your original code is that you were using = new ubyte[2] 
rather than = new ubyte[][2]. Hope this helps.

-- 
Robert
http://octarineparrot.com/


More information about the Digitalmars-d-learn mailing list