Dynamic cast fail

I’m trying to understand dynamic cast with a simple example.

Your text to link here…
As you can see that Pkt_h.Error = 0; // compiler error
So To prevent the compiler error, then I declared the “$cast” but I’ve got still have error.

class parent_class;
  bit [31:0] addr;

  function display();
    $display("Addr = %0d",addr);
  endfunction
endclass

class child_class extends parent_class;
  bit [31:0] data;

  function display();
    super.display();
    $display("Data = %0d",data);
  endfunction
endclass

module inheritence;
  initial begin
    parent_class p;
    child_class  c;
    c= new;
    c.data = 1;
    c.addr = 2;
    c.display();

    
    $cast(p,c);
    p.data = 3;
    p.addr = 4;
    p.display();

  end
endmodule

“data is not a class item”

Would you please let me know how to resolve this problem?

In reply to UVM_LOVE:

The “data” field is only existed in child class. You are unable to access it from parent class.
An class only can access its own attributes and its parent attributes.

I don’t know why you want to access “data” from parent_class?

In reply to chris_le:

In reply to UVM_LOVE:
The “data” field is only existed in child class. You are unable to access it from parent class.
An class only can access its own attributes and its parent attributes.
I don’t know why you want to access “data” from parent_class?

For understanding down-up casting.

You can’t because there is NO data in parent_class. Up-cast or down-cast, a variable (reference) can only access members defined in its scope.

In reply to UVM_LOVE: