SystemVerilog Datatype: Class (Part 3)

Previous: Inheritance and scope | Next: Virtual methods

So far...

Let us take a quick look into what we have learnt so far in Part 1 and Part 2 of this series.

Part 1:

We discussed the basic definitions and structures of a class data type. We learnt about object instance and object handle and various intricasies about object properties and object methods . Lastly, we went into static class properties and static class methods.

Part 2:

In this part, we looked into class constructors and how they can be used for shallow copying, among other things. We then turned to the topic of inheritance of the sub classes from their base class and how SystemVerilog resolves scope in such cases.

In this part we will start with another important aspect of classes in SystemVerilog - data encapsulation - and will see how other important properties of classes are based on this concept.

You can Run and You can Hide: Local and Protected Properties

So far, in all our examples, classes had members that were accessible to the rest of the program. However, this is not always desirable. For example, if a class has a constant property that should not be changed, making the variable publicly available may lead to an accidental change of the property. To avoid situation such as this, SystemVerilog provides ways for data encapsulation.

There are various ways to hide a class member (either property or method) from the outside world.

  • Use the keyword local: This will ensure that the member is available only to the method of the same class. Moreover, a local member will not be available even to the subclasses.
  • Use the keyword protected: This is similar to a local declaration, but a member will be visible inside subclasses.

In the following example, p1, p2 and p3 are protected members and cg is a local member of the class Triangle.

 
   class Triangle; 
      protected point p1, p2, p3;
      local     point cg;
      ... // other details
   endclass   

It is important to note that a local member is also accessible from a different instance of the same class.

Constant Class Property

A class property can be defined as read-only, by declaring it as const. In the following example, we declare p1, p2 and p3 as constant in addition to being protected.

   class Triangle;
      const protected point p1, p2, p3;
      ...
   endclass

A const declaration for a property can further be one of two types.

(a) A global constant, which assigns a value to the property at the time of the declaration. In the following code, p1 is an example of a global constant. A global constant can not be altered anywhere else.

   class Triangle;
      const protected p1 = {0,1}, p2, p3;
      ...
   endclass

(b) An instance constant, which has a const declaration, but the actual value assignment is done inside the constructor (i.e. the new() function) of the class. In the following example, p2 is an instance constant.

   class Triangle; 
      const protected p1 = {0,1}, p2, p3;
      ...
      function new;
         ...
         p2 = {3,4};
         ...
      endfunction
   endclass

Previous: Inheritance and scope | Next: Virtual methods

Share/Save/Bookmark



Verification Management
Join Verification Management Group


Shop Amazon - Contract Cell Phones & Service Plans

Book of the Month