typedef struct vs.Object-好处 [英] typedef struct vs. Object - Benefits
问题描述
我计划定义一个类,该类在其属性中包含x/y网格的坐标.但是,我不确定进行此设计的最佳"方法.这是一个非常简单的问题,我只想正确地做就可以了!
I plan to define a class, that among its properties contains coordinates for an x/y grid. However, I'm unsure of the 'best' way to approach the design of this. It's a very simple issue, I just want to do it correctly and have a justification!
一种解决方案是在对象中具有两个类型为 int
的属性,一个用于x,一个用于y.
One solution would be to have to two properties, of type 'int
', one for x and one for y, within the object.
另一个将定义一个包含x/y值的两个int的typedef结构,并将其命名为< ClassPrefix> Coordinate
.类似于 CGSize
?
The other would be define a typedef struct of two ints containing x/y values and naming it <ClassPrefix>Coordinate
. Similar to CGSize
?
还有其他/更好的方法吗?哪个是首选?不知道如何证明这两种方式.
Are there any other/better ways to do this? Which is preferred? Not sure how to justify either way.
谢谢!
蒂姆.
推荐答案
首选哪个?"的答案.问题取决于几个因素:
The answer to the "Which one is preferred?" question depends on a few factors:
- 您打算创建多少个这样的项目?-如果答案是数百万",则
struct
将获胜;如果答案是"58",则对象获胜. - 您需要在其上定义方法吗?-如果答案为是",则对象获胜;如果答案为是",则对象获胜.否则,
struct
可能没问题. - 您打算定义它的数组吗?-如果答案是是",则
struct
可能是一个更好的选择. - 您是否需要经常创建和销毁它?-如果答案是是",则
struct
可能是一个更好的选择.
- How many items like this you plan to create? - If the answer is "millions",
struct
wins; if the answer is "fifty eight", object wins. - Do you need to define methods on it? - If the answer is "yes", object wins; otherwise,
struct
may be OK. - Do you plan to define arrays of it? - If the answer is "yes",
struct
may be a better choice. - Do you need to create and destroy it often? - If the answer is "yes",
struct
may be a better choice.
最终,您的设计约束将帮助您确定最佳选择.没有普遍更好"的数据结构.
Ultimately, your design constraints help you determine what's best; there is no data structure that is universally "better".
这篇关于typedef struct vs.Object-好处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!