When are structs the answer?

前端 未结 12 900
广开言路
广开言路 2020-12-12 18:24

I\'m doing a raytracer hobby project, and originally I was using structs for my Vector and Ray objects, and I thought a raytracer was the perfect situation to use them: you

12条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-12 19:04

    While the functionality is similar, structures are usually more efficient than classes. You should define a structure, rather than a class, if the type will perform better as a value type than a reference type.

    Specifically, structure types should meet all of these criteria:

    • Logically represents a single value
    • Has an instance size less than 16 bytes
    • Will not be changed after creation
    • Will not be cast to a reference type

提交回复
热议问题