fre*_*ass 2 c++ type-conversion static-cast
如何将以下内容转换struct为unsigned char*?
typedef struct {
unsigned char uc1;
unsigned char uc2;
unsigned char uc3;
unsigned char uc5;
unsigned char uc6;
} uchar_t;
uchar_t *uc_ptr = new uchar;
unsigned char * uc_ptr2 = static_cast<unsigned char*>(*uc_ptr);
// invalid static cast at the previous line
Run Code Online (Sandbox Code Playgroud)
你不能在static_cast这里使用,因为类型之间没有关系.你必须使用reinterpret_cast.
基本上,static_cast在大多数情况下应该使用a ,而reinterpret_cast应该让你质疑为什么你这样做.
这是您可以使用的时间static_cast:
class Base {
};
class Derived : Base {
};
Base* foo = new Derived;
Derived* dfoo = static_cast<Derived*>( foo );
Run Code Online (Sandbox Code Playgroud)
而这里你可能需要一个reinterpret_cast:
void SetWindowText( WPARAM wParam, LPARAM lParam )
{
LPCTSTR strText = reinterpret_cast<LPCTSTR>( lParam );
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9468 次 |
| 最近记录: |