{"id":16324,"date":"2021-07-12T12:49:28","date_gmt":"2021-07-12T16:49:28","guid":{"rendered":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/?p=16324"},"modified":"2026-03-27T08:49:54","modified_gmt":"2026-03-27T12:49:54","slug":"class-variables-and-assignments-in-systemverilog","status":"publish","type":"post","link":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/2021\/07\/12\/class-variables-and-assignments-in-systemverilog\/","title":{"rendered":"Class Variables and Assignments in SystemVerilog"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p>Good OOP style says you should start your project with a common base class (or several). When you want to change its default behavior, extend it to make a new class by overriding methods and adding new properties. Everything in the base class is inherited so anything the base class can do, the extended class can also do. What is the relationship between the class variables that point to an object, and how can you assign between them?<\/p>\n\n\n\n<p>This is the fourth post in a series on OOP and UVM. Here is the previous <a href=\"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/2021\/06\/28\/runtime-checks-with-the-cast-method\/\" target=\"_blank\" rel=\"noreferrer noopener\">post <\/a>on $cast() with enumerated types. You might also want to read this <a href=\"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/2021\/06\/14\/systemverilog-class-variables-and-objects\/\" target=\"_blank\" rel=\"noreferrer noopener\">post <\/a>on Class Variables and Objects. Some people use &#8220;handle&#8221; for &#8220;class variable&#8221;. Whatever floats your boat!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Classy Cars<\/h2>\n\n\n\n<p>Start with a base class for an automobile. It has the property for the color, and a method drive().<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Automobile;\n  string color;\n  task drive();\n    ...\n  endtask\nendclass<\/pre>\n\n\n\n<p>This looks handy but sometimes you need to carry something big like a desk \u2013 you need a pickup truck! An important measure of a pickup is the size of its bed.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Pickup extends Automobile;\n  int bed_size;\nendclass<\/pre>\n\n\n\n<p>Notice that a Pickup is an Automobile. Everything you can do with an Automobile, like set the color, and call drive(), you can also do with a Pickup.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Mishandling<\/h2>\n\n\n\n<p>Declare some class variables. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Automobile a;    \/\/ Declare an Automobile class variable\nPickup p;        \/\/ And a Pickup variable<\/pre>\n\n\n\n<p>Can the variable <strong>a<\/strong> point to a Pickup object?<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">p = new();       \/\/ Construct a Pickup object\na = p;           \/\/ Legal? Yes!<\/pre>\n\n\n\n<p>Yes! What can you do with an Automobile object? You can call drive() and look at the color property. Those members exist in the Pickup object.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">a.drive();       \/\/ Yes \u2013 legal\na.color = \"red\"; \/\/ Yes \u2013 legal<\/pre>\n\n\n\n<p>Can the variable <strong>p<\/strong> point to an Automobile object?<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">a = new(); &nbsp;&nbsp;    \/\/ Construct an Automobile object\np = a; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/ ERROR: will not compile<\/pre>\n\n\n\n<p>The assignment won\u2019t even compile as an extended class handle can never point to a base object. If that was allowed, you could then have the following code that tries to access the <strong>bed_size<\/strong> property in an Automobile object. The code will compile, but there is no such property.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">p.bed_size = 64; \/\/ ERROR: No bed_size in Automobile object<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Relationship between class variable<\/h2>\n\n\n\n<p>Here is a diagram of the two classes, and the variables that point to their objects.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"428\" src=\"https:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/54\/2021\/07\/4_1_venn-1024x428.png\" alt=\"Classes and variables\" class=\"wp-image-16329\" srcset=\"https:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/54\/2021\/07\/4_1_venn-1024x428.png 1024w, https:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/54\/2021\/07\/4_1_venn-600x251.png 600w, https:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/54\/2021\/07\/4_1_venn-768x321.png 768w, https:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/54\/2021\/07\/4_1_venn-900x376.png 900w, https:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/54\/2021\/07\/4_1_venn.png 1368w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Classes and variables<\/figcaption><\/figure>\n\n\n\n<p>You can see that an Automobile variable can point to both Automobile and Pickup object, but a Pickup variable can only point to a Pickup object. The range of values for the base class variable is larger than the extended variable\u2019s range. That didn\u2019t make sense to me until I made the above Venn diagram.<\/p>\n\n\n\n<p>Here are the rules for assigning between base and derived class variables. <\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/54\/2021\/07\/4_2_variable_rules.png\" alt=\"Assigning base and extended class variables\" class=\"wp-image-16334\" width=\"436\" height=\"74\" srcset=\"https:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/54\/2021\/07\/4_2_variable_rules.png 735w, https:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/54\/2021\/07\/4_2_variable_rules-600x102.png 600w\" sizes=\"auto, (max-width: 436px) 100vw, 436px\" \/><figcaption>Rules for assigning base and extended class variables<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>This post introduces the concept of the legal values for a class variable, for both a base and extended object. Stay tuned next time when I lose my car in the parking lot, and one more rule.<\/p>\n\n\n\n<p>Try this code with your favorite simulator. Lean forward, make mistakes, get messy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Good OOP style says you should start your project with a common base class (or several). When you want&#8230;<\/p>\n","protected":false},"author":72251,"featured_media":16329,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spanish_translation":"","french_translation":"","german_translation":"","italian_translation":"","polish_translation":"","japanese_translation":"","chinese_translation":"","footnotes":""},"categories":[1,982,10],"tags":[300,951,382,383,950,952,953,515,585,594,612,614,648,751],"industry":[],"product":[],"coauthors":[942],"class_list":["post-16324","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-news","category-systemverilog","category-tips-tricks","tag-cast","tag-base-class","tag-class","tag-class-handles","tag-class-variable","tag-derived-class","tag-extended-class","tag-handle","tag-member","tag-method","tag-object-oriented-programming","tag-oop","tag-property","tag-systemverilog"],"featured_image_url":"https:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/54\/2021\/07\/4_1_venn.png","_links":{"self":[{"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/posts\/16324","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/users\/72251"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/comments?post=16324"}],"version-history":[{"count":4,"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/posts\/16324\/revisions"}],"predecessor-version":[{"id":16385,"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/posts\/16324\/revisions\/16385"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/media\/16329"}],"wp:attachment":[{"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/media?parent=16324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/categories?post=16324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/tags?post=16324"},{"taxonomy":"industry","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/industry?post=16324"},{"taxonomy":"product","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/product?post=16324"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/verificationhorizons\/wp-json\/wp\/v2\/coauthors?post=16324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}