Access static class property from different modules

Hi,

I’m trying to access a static property of a class from different modules, but it doesn’t seem that a static property is shared with class instances located in another module in different file.

For example:


class my_class;
static int count = 0;
function void set_cnt(input int cnt);
    count = cnt;
endfunction
endclass

module test ();
    mod1  mod1();
    mod2  mod2();
endmodule

module mod1 ();
my_class c1;
initial begin
    c1 = new();
    c1.set_cnt(10);
    $display (" the count in c1 is %d", c1.count);
end
endmodule

module mod2 ();
my_class c2;
initial begin
    c2 = new();
    #1000;
    $display (" the count in c2 is %d", c2.count);
end
endmodule

If all of above is in one file, the result is correct: the value of c2.count is 10. But if module mod1, mod2, and test are in their own files, the value of c2.count will be 0.

Could anyone tell me why is that?

Thanks,
Simon

You didn’t say how you compiled my_class when you split up the files, but I’m assuming you compiled it with each module. Then you get three independent types of my_class. Although they have the same name, they are independent class types because the full name of any user defined type includes the scope containing the declaration. The scope outside of a module is called $unit, and each file has a unique compilation unit scope. You need to put the class declaration in a package and import the package into each module declaration. See http://go.mentor.com/package-import-versus-include .

Dave

In reply to dave_59:

Hi Dave,

Thank you very much for the answer. You’re correct that I used “`include my_class.sv” instead of a package. After I changed it to a package, as you suggested, everything looks fine!

The link doesn’t work for me, so I googled it and find it here: http://blogs.mentor.com/verificationhorizons/blog/2010/07/13/package-import-versus-include/. It is a great article.

Thank you again for taking time to answer my question.

Simon

In reply to simonz:

I fixed the link by removing the “.” at the end of the sentence.