Casting back to more specialised interface

前端 未结 2 1888
情话喂你
情话喂你 2021-01-30 05:14

I\'m writing a game in go. In C++ I would store all my entity classes in an array of the BaseEntity class. If an entity needed to move about in the world it would be a PhysEntit

相关标签:
2条回答
  • 2021-01-30 05:27

    Specifically, the Go "interface" type has the information on what the object really was, that was passed by interface, so casting it is much cheaper than a C++ dynamic_cast or the equivalent java test-and-cast.

    0 讨论(0)
  • 2021-01-30 05:41

    Use a type assertion. For example,

    original, ok := entity.(PhysEntity)
    if ok {
        println(original.b())
    }
    
    0 讨论(0)
提交回复
热议问题