是它的typedef指针是一个好主意? [英] Is it a good idea to typedef pointers?
问题描述
我通过一些code看了看,发现该公约是把指针类型,如
I looked through some code and noticed that the convention was to turn pointer types like
SomeStruct*
到
typedef SomeStruct* pSomeStruct;
有什么功德呢?
推荐答案
这可以在适当的时候指针本身可以看作是一个黑箱,也就是一个数据的内部重组presentation应是风马牛不相及的code。
This can be appropriate when the pointer itself can be regarded as a "black box", that is, a piece of data whose internal representation should be irrelevant to the code.
从本质上讲,如果你的code会的从不的取消引用指针,而你只是通过它周围的API函数(有时引用),则不仅typedef的减少<$数C $ C> * 在你的code,但也表明该指针真不该插手与程序员。
Essentially, if your code will never dereference the pointer, and you just pass it around API functions (sometimes by reference), then not only does the typedef reduce the number of *
s in your code, but also suggests to the programmer that the pointer shouldn't really be meddled with.
此也可以更容易地改变API中的将来在需要时,例如使用的ID,而不是一个指针(或反之亦然)。由于指针从未应该首先被废弃时,现有的code不会打破。
This also makes it easier to change the API in the future if the need arises, for instance using an ID rather than a pointer (or vice versa). Since the pointer was never supposed to be dereferenced in the first place, existing code won't break.
这篇关于是它的typedef指针是一个好主意?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!