如何从 systemverilog 中父 class 对象中的对象访问子 class 中的变量

How to access a variable in child class from an obj in parent class object in systemverilog

所以我想修改一个变量,它不直接在父 class 中,而是在 class 中,它在父 class 中实例化。例如

class cfg;

    int a  

endclass

class parent;

    cfg cfg1

endclass
class child extends parent;

    <how to change variable "a" here which is declared in cfg?>

endclass

您的第一个问题是使用术语 parentchild 关于 inheritance。这些术语意味着两个不同的对象。您拥有的是 base class 类型和 derived class 类型。构造派生 class 对象时,您可以访问基础 class 中的所有内容,就好像它们都在同一个 class.

中一样
class cfg;

    int a  

endclass

class base;

    cfg cfg1

endclass
class derived extends base;

    //<how to change variable "a" here which is declared in cfg?>
    //Answer: Just use cfg1.a
endclass