About :associative array :class index type

Hello All,
I cannot able to declare the class type based index in the associative array.
It is shown in the LRM, [Indices can be objects of that particular type or derived from that type. Any other type is illegal and shall result in a type check error. ]

according to the word of LRM,
what I have coded is given below,

module test;
 int class_ name[obj];
class AB;
 int a;
endclass
 initial
  begin
   AB obj=new;
class_name[obj]=10;
  $display("%d",class_name[obj]);
end
endmodule

Tell me how to access variable a? & whether my way of approach is right?

In reply to SANJAIKUMAR:

  1. In your associative array declaration, you have specified a class variable obj, not the required class type AB, as the index type. See this for the difference.
  2. Your array declaration needs to come after declaring the class type.
  3. Access the class member a using class_name[obj].a

In reply to dave_59:

Thanks for the explanation. Got your point.

Hi Dave,
I tried it :


module class_var;

class AB;
int a;

endclass
int arr[AB];
AB obj;

initial
begin
arr[obj.a]=20;
$display("%0d\n",arr[obj.a]);
end
endmodule

but it is genrating fatal error,illegal associative array key type

In reply to shobhana.swarnkar18:

obj is a class variable whose type is AB. obj.a is a class member variable whose type is int.

If you declared the associative array with
int arr[AB]
, you want to the index to be a constructed object of
AB
. The values of the class member variables are not relevant here.

initial
  begin
    obj = new();
    obj.a = 5; // irrelevant 
    arr[obj]=20;
    $display("%0d\n",arr[obj]); // should display 20
  end

In reply to dave_59:

Hi Dave,
Can you please help how can i do first,last,prev and next operation in clas_index associative array.

Thanks

In reply to sv_uvm_beginner:

Using those methods with a class index does not make a lot of sense—you should be using a foreach loop. But other than the fact that the ordering is arbitrary, each method works the same as if you had used an integral index type.