accessing derived class properties using base class object in system verilog
I am unable to access the derived classproperty using base class handle? Is it legal , I am in confusion ?
class A;
int i;
endclass
class B extends A;
byte j;
endclass
A a;
B b;
module type_cast;
initial begin
a = new();
b = new();
a = b;
$cast(b,a);
b.j = 8;
a.j = 2;
$display("a is",a.j);
end
endmodule
** Error: type_cast.sv(20): Field/method name (j) not in ‘a’
You can only refer to class properties from a class variable defined by its class type. Let’s look at your initial block line by line.
a = new();
You construct a class object of the class type A. This object has only one class property:i. The object handle is stored in the class variable a.
b = new();
You construct a class object of the class type B. This object has two class property:iand j. The object handle is stored in the class variable b.
a = b;
You assign the class handle stored in the variable b to a. Now both variables reference the same object, the one you constructed of type B. The object handle that used to be stored in variable a of type A is no longer available and that object will be removed.
$cast(b,a);
This checks that the type of the object referenced by a is compatible the type of class variable b. Had you not had the previous a=b; statement above, this would have produced an error. The $cast ensures that variable b contains a handle to an object of type B or any type derived fromB.
b.j = 8;
This statement is always legal as long as b is not null. The compiler assures us that the only kinds of objects that b can reference are of type B or an extension of B. That type of object will always have a class property j.
a.j = 2;
$display(“a is”,a.j);
These statements are always illegal because class variable a has no knowledge of types other than class A. Because the compiler allows only certain kinds of assignments to the variable a, it can’t guarantee that it will always contain a class object with a property j.
Your example is trivial with no conditional branching. The compiler does not look at your code in total and try to prove that a had a type B object at the time it get executed.
In reply to dave_59:
then how to access the child property using parent handle?
and exlplain in detail the use of $cast??
In reply to sagar@aceic.com:
Do not use the terms parent and child when talking about inheritance. Use base and extended class. Also, you access class properties using a class variable. A class variable stores a handle to class object. The handle type always matches the object type. A base class variable can only access properties defined in the base class type, or whatever class the base class was extended from. A base class variable knows nothing about the properties of class object that may be extended from it. It’s like getting an OS update for your Phone/Tablet/PC. That OS may have been extended to support new features for hardware you old device doesn’t have. You won’t be able to access those features unless you upgrade (up-cast) your hardware.
However, the same is not true for virtual methods. Calling a virtual method from a base class variable always calls the virtual method defined by the type of class handle stored in the class variable. See my blog for more class terminology. Since the virtual method of an extended class type has access to extended class properties, calling a virtual method from a base class variable gives you indirect access to those extended class properties.