• SV-- cast类型转换练习(8/26)


            在SV中,cast作为一个显示的转换方法,一个重要的应用就是在类继承过程中实现类型的向下转换;

            向上转换:将扩展类句柄(child_handle) 赋给 父类(father_handle)

            father_handle = child_handle

            向下转换 :将父类句柄(father_handle) 赋给 扩展类(child_handle)
            child_handle = father_handle

            在SV中,编译允许基类句柄指向子类对象,这是合法的;但是不允许子类句柄指向基类对象,这样可能会造成数据的丢失,是不合法的,在SV编译时会检查句柄类型,会报错。

            类型向下转换需要使用cast,cast是检查对象类型,而不是检查句柄类型。

    实验代码:

    1. class father;
    2. string m_name;
    3. function new(string name);
    4. m_name = name;
    5. endfunction
    6. function void print();
    7. $display("HELLOW %s",m_name);
    8. endfunction
    9. endclass:father
    10. class child0 extends father;
    11. string car="car";
    12. function new(string name);
    13. super.new(name);
    14. endfunction
    15. endclass:child0
    16. class child1 extends father;
    17. string plane="plane";
    18. function new(string name);
    19. super.new(name);
    20. endfunction
    21. endclass:child1
    22. module top;
    23. father f;
    24. child0 c0;
    25. child1 c1;
    26. child1 c2;
    27. initial begin
    28. f=new("father");
    29. f.print();
    30. c0=new("child0");
    31. f=c0;
    32. f.print();
    33. c1=new("child1");
    34. f=c1;
    35. f.print();
    36. c1.plane="big_plane";
    37. $cast(c2,f);
    38. f.print();
    39. $write(",has %s", c2.plane);
    40. end
    41. endmodule:top

     仿真结果:

     代码解析:

    f=new("father");//为父类开辟空间,此时m_name为father

    f.print(); //调用函数print 打印Hellow father

    c0=new("child0");//为child0子类开辟空间,此时car=car,m_name=child0;

    f=c0;//父类句柄指向子类child0对象,父类car=car,m_name=child0;

    f.print();//调用函数print打印Hellow child0

    c1=new("child1");//为child0子类开辟空间,此时plane=plane,m_name=child1;

    f=c1; /父类句柄指向子类child0对象,父类plane=plane,m_name=child1;

    f.print();//调用函数print打印Hellow child1        

    c1.plane="big_plane";//显式将c1.plane赋值为big_plane,此时父类f.plane也赋值为big_plane

    $cast(c2,f);//将c2悬空的句柄指向c1,c2.plane=big_plane,m_name=child1

    f.print();//调用函数print打印Hellow child1

    $write(",has %s", c2.plane);//打印,has big_plane

    打印结果: 

    上边的练习做完了,但是cast的魅力你真的有感受到吗?可能还真没有,那就让我们通过下边的代码再细细感受一下cast到底做了什么,她的魅力究竟在何处!

     首先进行一个简单的代码修改:

    1. class father;
    2. string m_name;
    3. function new(string name);
    4. m_name = name;
    5. endfunction
    6. function void print();
    7. $display("HELLOW %s",m_name);
    8. endfunction
    9. endclass:father
    10. class child0 extends father;
    11. string car="car";
    12. function new(string name);
    13. super.new(name);
    14. endfunction
    15. endclass:child0
    16. class child1 extends father;
    17. string plane="plane";
    18. function new(string name);
    19. super.new(name);
    20. endfunction
    21. endclass:child1
    22. module top;
    23. father f;
    24. child0 c0;
    25. child1 c1;
    26. child1 c2;
    27. initial begin
    28. f=new("father");
    29. f.print();
    30. c0=new("child0");
    31. f=c0;
    32. f.print();
    33. c1=new("child1");
    34. f=c1;
    35. f.print();
    36. $display("f.plane=%s",f.plane);
    37. //c1.plane="big_plane";
    38. //$cast(c2,f);
    39. //f.print();
    40. //$write(",has %s", c2.plane);
    41. end
    42. endmodule:top

    修改内容:

    去掉cast代码,加入一行代码: $display("f.plane=%s",f.plane);

    要求打印出 f.plane;

    编译结果会报错:

     这是因为在SV编译时会检查句柄类型,而不是对象类型,会报错。所以就出现了练习的代码,使用cast进行对象转换,再对子类变量进行访问;

    可能有的小伙伴还是很迷茫,为什么要进行两部操作,直接通过c1去访问不就好了,有什么应用场景呢?由于博主实际项目经验较浅,这部分内容待后续补上,感兴趣的小伙伴可以自己去搜一下,或者已经了解的小伙伴,也可以分享出来哦,大家一起进步!

     

  • 相关阅读:
    vue集成百度地图vue-baidu-map
    Chrome vs Firefox 性能之争,到底哪家强?
    HamsterBear F1C200s Linux v5.17 RTL8188EUS 适配
    ciscn 2022 华东北分区赛pwn duck
    php实现选择排序法
    Go语言的100个错误使用场景(48-54)|错误管理
    Linux 安装 nvm,并使用 Jenkins 打包前端
    Postman下发流表至Opendaylight
    集合的自反关系和对称关系
    SpringBoot整合PDF动态填充数据并下载
  • 原文地址:https://blog.csdn.net/weixin_45680021/article/details/126534553