Sei sulla pagina 1di 4

8/2/2017 What is Differance between downcasting and upcasting Systemverilog ?

| Verification Academy

Verification Academy (/) Search form


Register >
Search Verification Academy
Log In (/user/login?destination=forums/systemverilog/what-differance-between-downcasting-and-upcasting-
systemverilog)
Topics Courses Forums Patterns Library Cookbooks Events More

Ask a Question (/user/login?destination=ask-a-question)

SystemVerilog
Search the Forums
(/forums/all-topics)

Home (/) / Forums (/forums) / SystemVerilog (/forums/systemverilog) /


What is Differance between downcasting and upcasting Systemverilog ?

What is Differance between downcasting and upcasting


Systemverilog ?
SystemVerilog 2299 (/forums/systemverilog)

Balu_15


Full Hi Everone ,
Access
54 posts I was googling for Defination of upcasting and downcasting in SV. the search result is making confuse
? Please clear this concept.
May 15, 2014 at 12:35 am
Regards
kbkdec15

Replies Order by: Newest Last Log In to Reply (/user/login?destination=node/40692)

mpattaje


Full In reply to kbkdec15 (https://verificationacademy.com/forums/systemverilog/what-differance-between-
Access downcasting-and-upcasting-systemverilog#question-40692):
8 posts
Hi kbkdec15,
February 16, 2017 at 4:46 pm
I hope by this time you must have found the answer for this.
(https://verificationacademy.com/forums/systemverilog/what-
differance-between- We take the advantage of inheritence and polymorphysm of SV classes here. Upcasting is where we
downcasting-and-upcasting- assign the derived class's handle to base class. The advantage is obvious: reusability.
systemverilog#answer-59613)

https://verificationacademy.com/forums/systemverilog/what-differance-between-downcasting-and-upcasting-systemverilog 1/4
8/2/2017 What is Differance between downcasting and upcasting Systemverilog ? | Verification Academy

class base;
int a = 5;
endclass

class child extends base;


int b = 6;
endclass

module top;
initial begin
base base_h;
child child_h;
child_h = new();
$cast(base_h, child_h); // same as: base_h = child_h;
$display(child.a); // a is base's property
end
endmodule

Now downcasting is opposite of the above; assigning the base class handle to child class handle. But
this will not work until the base class handle already refers to child class handle. When an array of
base class is present, where it is filled with different child class handles, this downcasting comes
handy. Check the following code (copied from http://stackoverflow.com/questions/20548473/does-
systemverilog-support-downcasting (http://stackoverflow.com/questions/20548473/does-
systemverilog-support-downcasting)):

https://verificationacademy.com/forums/systemverilog/what-differance-between-downcasting-and-upcasting-systemverilog 2/4
8/2/2017 What is Differance between downcasting and upcasting Systemverilog ? | Verification Academy

class animal;
function void eat();
endfunction
endclass

class dog extends animal;


function void bark();
$display("woof");
endfunction
endclass

class cat extends animal;


function void meow();
$display("meow");
endfunction
endclass

module test;

initial begin
dog a_dog = new();
cat a_cat = new();

animal animals[$];
animals.push_back(a_dog);
animals.push_back(a_cat);

foreach (animals[i]) begin


dog another_dog;
animals[i].eat();
if ($cast(another_dog, animals[i])) begin
$display("Found a dog!");
another_dog.bark();
end
end

end
endmodule

Hope that helps.


-mpattaje

8Blades


Full In reply to mpattaje (https://verificationacademy.com/forums/systemverilog/what-differance-between-
Access downcasting-and-upcasting-systemverilog#reply-59613):
20 posts
The casting in the first example is redundant as the child already have the variable a, as it inherits all
February 16, 2017 at 11:27 pm properties of the base class. So, if the casting wasn't done, still the access of child_h.a is valid.
(https://verificationacademy.com/forums/systemverilog/what-
Upcasting is basically used with virtual methods, where you want to access base class methods using
differance-between-
the variable of the superclass. Below example will clarify the idea:
downcasting-and-upcasting-
systemverilog#answer-59623)

https://verificationacademy.com/forums/systemverilog/what-differance-between-downcasting-and-upcasting-systemverilog 3/4
8/2/2017 What is Differance between downcasting and upcasting Systemverilog ? | Verification Academy

class BasePacket;
int A =1;
int B = 2;
function void printA;
$display("BasePacket::A is %d", A);
endfunction : printA
virtual function void printB;
$display("BasePacket::B is %d", B);
endfunction : printB
endclass : BasePacket

class My_Packet extends BasePacket;


int A = 3;
int B = 4;
function void printA;
$display("My_Packet::A is %d", A);
endfunction: printA
virtual function void printB;
$display("My_Packet::B is %d", B);
endfunction : printB
endclass : My_Packet

program main;
BasePacket P1 = new;
My_Packet P2 = new;
initial begin
P1.printA; // displays 'BasePacket::A is 1'
P1.printB; // displays 'BasePacket::B is 2'
$case(P1,P2) // same as P1 = P2.. P1 has a handle to a My_packet object
P1.printA; // displays 'BasePacket::A is 1'
P1.printB; // displays 'My_Packet::B is 4' latest derived method
P2.printA; // displays 'My_Packet::A is 3'
P2.printB; // displays 'My_Packet::B is 4'
end
endprogram: main

Mentor, a Siemens Business, All Sitemap (/sitemap)


rights reserved Terms & Conditions (https://verificationacademy.com/terms-and-conditions)
Verification Horizons Blog (http://www.verificationhorizonsblog.com)
LinkedIn Group (http://www.linkedin.com/groups/Verification-Academy-4668056)

https://verificationacademy.com/forums/systemverilog/what-differance-between-downcasting-and-upcasting-systemverilog 4/4

Potrebbero piacerti anche