Local constructor

Hi,
In LRM it is mentioned that “a constructor can be declared as local or protected”. But the object for the same is not getting created. Can anybody let me know when can these local/protected constructors be used.
Please find the following code.

<font size=10>class a;
  int a;
  local function new();
    a = 10;
  endfunction
endclass
module top;
  a a_h;
  initial begin
    a_h = new();
  end
endmodule</font>

The compiler is throwing error “illegal local constructor”.

Raja,

A local or protected constructor is typically used when creating a singleton pattern class. The singleton pattern is used when you need to limit the construction of a class to a single object. In SystemVerilog, this looks like

class A;
  local static A m_h;
  local function new();
  endfunction
  static function A get_A();
    if (m_h == null)
        m_h = new();
    return m_h;
  endfunction
endclass

This class can only be constructed by calling the static function get_A, and it is guaranteed to be constructed only once on the first call to get_A. This style of coding also avoids the static initialization order fiasco by constructing on first use.

Dave Rich

In reply to sysadmin:

Hi Dave,
So now this singleton pattern can be used as a global object from which the object properties can be accessed across the environment. Is it right?

-Raja