typedef 指针是个好主意吗? [英] Is it a good idea to typedef pointers?

查看:51
本文介绍了typedef 指针是个好主意吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查看了一些代码并注意到约定是将指针类型转换为

I looked through some code and noticed that the convention was to turn pointer types like

SomeStruct* 

进入

typedef SomeStruct* pSomeStruct;

这有什么好处吗?

推荐答案

当指针本身可以被视为一个黑盒",即内部表示应该与代码.

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.

本质上,如果您的代码从不取消引用指针,而您只是将它传递给 API 函数(有时是通过引用),那么 typedef 不仅会减少 *s 在您的代码中,但也向程序员建议不应真正干预指针.

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 而不是指针(反之亦然),现有代码不会中断,因为从一开始就不应该取消引用指针.

This also makes it easier to change the API in the future if the need arises. For instance, if you change to using an ID rather than a pointer (or vice versa) existing code won't break because the pointer was never supposed to be dereferenced in the first place.

这篇关于typedef 指针是个好主意吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆