先陈述一下情况。我看C++相关的书,最近好像类型转换都不直接用(type)var这样的形式了。但是照着书上写VC的时候却出现了错误。
//GetMenu()->AppendMenuA(MF_POPUP, static_cast<UINT>(menu.m_hMenu), "对话框");编译不能通过
GetMenu()->AppendMenuA(MF_POPUP, (UINT)(menu.m_hMenu), "对话框");//这个就能通过后来我换成这样,也成功了。
GetMenu()->AppendMenuA(MF_POPUP, reinterpret_cast<UINT>(menu.m_hMenu), "对话框");我的疑问就是,如果2个类型能进行转换的话,应该static_cast在编译期间都可以。reinterpret是无视类型的转换,难道UINT和HMENU 2个类型完全不相关而必须进行这样的转换,我觉得这样的可能性不大。
UINT是unsigned int, 而HMENU我找到下面的就不知道了。不知道他是个什么东西。
#if !defined(_MAC) || !defined(WIN_INTERNAL)
DECLARE_HANDLE(HMENU);
#endif这个情况怎么解释呢,另外各位一般写程序的时候是怎么进行转换的?请大家帮助我

解决方案 »

  1.   

    static_cast只适用于被转换类型之间有继承关系的,UINT和HMENU当然没有,你也不能这么用
      

  2.   

    不知道有继承关系这个从何而来,听到这句话我很惊讶。那么以下这个如何解释呢。float happy_var = 3.0f;
    int sad_var = static_cast<int>(happy_var);
      

  3.   

    No run-time type check is made to help ensure the safety of the conversion.The static_cast operator can be used for operations such as converting a pointer to a base class to a pointer to a derived class. Such conversions are not always safe.In general you use static_cast when you want to convert numeric data types such as enums to ints or ints to floats, and you are certain of the data types involved in the conversion. static_cast conversions are not as safe as dynamic_cast conversions, because static_cast does no run-time type check, while dynamic_cast does. A dynamic_cast to an ambiguous pointer will fail, while a static_cast returns as if nothing were wrong; this can be dangerous. Although dynamic_cast conversions are safer, dynamic_cast only works on pointers or references, and the run-time type check is an overhead. See dynamic_cast for more details.
      

  4.   

    恩,不好意思,回去翻了翻书,看到书上是这样说的:static_cast包括典型的非强制变换,窄化转换,使用void×的强制转换,隐式类型转换和类层次的静态定位。但是我到现在有些基本概念也不明白。
    1隐式类型转换就是不需要强制类型转换的自动转换么?
    2那么非强制类型转换又什么意思?
    3类层次的静态定位又是什么东西?家里茶饭不思,关机断水,在线苦等!!
      

  5.   

    1。 对
    2。人家没提你说的这个词啊?人家说的是从void*转换为其他指针类型吧?
    3. 例如从基类指针转换为派生类指针HMENU应该是void*把,static_cash不能把指针转换为整型,大概这是你代码失败的原因
      

  6.   

    谢谢楼上,从基类指针转换为派生类指针不是dynamic_cast么,那dynamic_cast又何特殊之处?
      

  7.   

    这种问题随便google一下不就知道了?再说我3楼不是已经贴了?