Ready-to-run snippets

Code Examples

Ready-to-run Verilog, SystemVerilog, and UVM examples. Click any to open it live in the IDE.

← All examples

When Accessed By Child Classes

SystemVerilog Advanced Features

A local member of a base class is invisible even to a derived class, so the child method reading it fails to compile.

Open in IDE
testbench.sv
// Define a base class and let the variable be "local" to this class
class ABC;
  local byte local_var; 	
endclass

// Define another class that extends ABC and have a function that tries
// to access the local variable in ABC
class DEF extends ABC;  
  function show();
    $display ("local_var = 0x%0h", local_var);
  endfunction
endclass

module tb;
  initial begin
    
    // Create a new object of the child class, and call the show method
    // This will give a compile time error because child classes cannot access
    // base class "local" variables and methods
    DEF def = new();
    def.show();

  end
endmodule




/*
Simulation Log:
---------------

irun: 15.20-s038: (c) Copyright 1995-2017 Cadence Design Systems, Inc.
    DEF def = new();
            |
ncvlog: *W,VARIST (testbench.sv,20|12): Local static variable with initializer requires 'static' keyword.
    def.show();
           |
ncvlog: *W,FUNTSK (testbench.sv,21|11): function called as a task without void'().
    $display ("local_var = 0x%0h", local_var);
                                           |
ncvlog: *E,CLSNLO (testbench.sv,10|43): Access to local member 'local_var' in class 'ABC' is not allowed here.
irun: *E,VLGERR: An error occurred during parsing.  Review the log file for errors with the code *E and fix those identified problems to proceed.  Exiting with code (status 1).
*/
design.sv
// Code your design here
Admin mode — edit and delete controls are visible on each row. Use the IDE to add new snippets via File → Save as Snippet.