Jam*_*lis 13
您可以使用a char*
来访问C++中的任何类型的对象,因此:
struct S
{
int a;
int b;
// etc.
};
S my_s;
char* my_s_bytes = reinterpret_cast<char*>(&my_s);
// or, if you prefer static_cast:
char* my_s_bytes = static_cast<char*>(static_cast<void*>(&my_s));
Run Code Online (Sandbox Code Playgroud)
(关于对比的正确性至少存在一些争论 ;在实践中它并不重要 - 两者都应该产生相同的结果)reinterpret_cast
static_cast
I like to use a union.
typedef struct b {
unsigned int x;
unsigned int y;
} b_s;
typedef union a {
b_s my_struct;
char ary[sizeof(b_s)];
} a_u;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
30348 次 |
最近记录: |