当在现代C ++应用程序中使用Win32代码时,应该使用正确的转换吗? [英] When using Win32 code in your modern C++ app, should you use proper casting?
问题描述
例如,可以在整个MSDN文档中找到以下类型:
For example the following cast can be found littered throughout the MSDN documentation:
(LPTSTR)&lpMsgBuf
我应该把它转换成:
static_cast<LPTSTR>(&lpMsgBuf);
或者我应该离开所有常用的C-esque Win32部分,因为它们通常在文档中找到,并保存更为惯用的C ++风格/用于其余的我的代码?
Or should I just leave all the idiomatic C-esque Win32 portions as they are typically found in the docs, and save the more idiomatic C++ style/usage for the rest of my code?
推荐答案
:
因此使用它们。
更多说明,我们的意思是,你不能只是投射到某事,你必须说为什么你正在投射(我在一个继承层次结构),我的演员是实现定义和可能不可移植(reinterpret_cast),我抛弃constness(const_cast)等)。
By more explanatory, we mean that you can't just cast to something, you have to say why you're casting (I'm casting in an inheritance hierarchy (dynamic_cast), my cast is implementation defined and possibly not portable (reinterpret_cast), I'm casting away constness (const_cast), etc.).
他们有目的地长而丑陋,使得演员在读者身上跳出来(并劝阻使用太多演员的编程风格)。
They're purposefully long and ugly so that the cast jumps out at the reader (and to discourage a programming style that employs too much casting).
它们更安全,因为例如,如果没有明确这样做,你就不能抛弃constness。
They're safer because, e.g., you can't cast away constness without explicitly doing that.
这篇关于当在现代C ++应用程序中使用Win32代码时,应该使用正确的转换吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!