我应该避免typedef,尝试使用原始名称和cast可能吗? [英] Should I avoid typedef, try to use primitive names and cast when possible?

查看:185
本文介绍了我应该避免typedef,尝试使用原始名称和cast可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定这里的词汇,但希望我能使自己理解。



由于我正在通过winapi与一个小于 - 关于C ++的坚如磐石的知识,我发现很多typedef的东西,对我来说,似乎过度复杂的问题,并添加一个我要记住的事情。



例如, UINT 而不是 unsigned int HBITMAP out只是一个 HANDLE 和很多其他人。



我的问题是,可以/应该替换更多如果可能的话,可以使用泛型版本的类型,并在需要的时候将其强制转换(这是什么叫做)。



例如, p>


  • void SomeFunction(unsigned int some_int){...} void SomeFunction(UINT some_int){...}


  • HANDLE hBMP = LoadImage(...); ImageList_Add(...(HBITMAP)hBMP ...); 而不是 HBITMAP hBMP = ...



这对新手,一般不好的做法有什么好处吗?

解决方案

1)Win32 API实际上是C,而不是C ++。



如果你考虑像MFC或ATL其中是面向对象,仅C ++的API。



2)微软喜欢使用大量的宏和typedef。我不能说这是好还是坏。这只是一个生活的事实。



3)最重要的是 - 你应该肯定遵循微软的约定,当你使用Windows的时候,您使用Microsoft API。使用HANDLE当MSDN页面上显示HANDLE是一个好东西。类似的建议适用于LPxxx,TRUE,FALSE,INVALID_HANDLE,HRESULT等等。如果这是它在MSDN中说的,那就是你应该使用。



它不仅会使你的代码更可读...但令人惊讶的是,它也可以防止微妙的错误,你可能会导致第二猜测真实类型。



不要 尝试第二次猜类型。



遵循标准约定会使生活更轻松,更安全,更可靠,更便携。



IMHO ...


I'm not sure of the vocabulary here, but hopefully I can make myself understood.

As I'm working through the winapi with a less-than-rock-solid knowledge of C++, I find a lot of typedef stuff that, for me, seems to overcomplicate the issue and add one more thing I have to remember.

For example, UINT instead of unsigned int, HBITMAP which turns out is just a HANDLE, and lots of others.

My question is, can / should I substitute the more generic version of the type when possible, and just cast it down when it's needed (and, what's this called)?

For example, I'd like to write

  • void SomeFunction(unsigned int some_int) { ... } instead of void SomeFunction(UINT some_int) { ... }

  • HANDLE hBMP = LoadImage(...); ImageList_Add(... (HBITMAP) hBMP ...); instead of HBITMAP hBMP = ...

Is this good for newcomers, bad practice in general, or what?

解决方案

1) The Win32 API is actually C, not C++.

The distinction becomes more important if you consider stuff like MFC or ATL, both of which are "object-oriented", C++-only APIs. And both of which are mercifully becoming (have become?) obsolete.

2) Microsoft likes to use lots of macros and typedefs. I can't say whether that's "good" or "bad". It's simply a fact of life. And it'll become second nature to you if you work with Windows for any length of time.

3) Most importantly - Yes: you should definitely follow the Microsoft conventions when you use the Microsoft APIs. Using "HANDLE" when the MSDN page says "HANDLE" is a Good Thing. Similar advice holds for "LPxxx", "TRUE", "FALSE", "INVALID_HANDLE", "HRESULT" etc etc. If that's what it says in MSDN, then that's what you should use.

Not only will it make your code more readable ... but, surprisingly often, it can also prevent subtle bugs you might cause by "second guessing" the "true type".

Do not try to "second guess" the types. It's just a Bad Idea.

Following the standard conventions will make life easier, safer, more reliable and more portable.

IMHO...

这篇关于我应该避免typedef,尝试使用原始名称和cast可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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