B
Bob Hairgrove
Surely this is a no-brainer, but sometimes I think I must have no
brain (no comments, please<g>). Anyway, I have the following struct
containing bit fields:
struct ExtPenStyle
{
unsigned long style : 4;
unsigned long : 4; // unused
unsigned long endcap : 2;
unsigned long : 2; // unused
unsigned long join : 2;
unsigned long : 2; // unused
unsigned long type : 1;
unsigned long : 15; // unused
};
I tried casting from/to unsigned long using static_cast,
reinterpret_cast and C-style cast. The compiler refuses to do it, but
I need to call API code which expects an unsigned long with bitmapped
values.
I know I could use a union or memcpy(), but I wonder if there is a
cleaner way? Thanks.
#include <iostream>
#include <ostream>
// struct ExtPenStyle
// { see above };
int main()
{
using std::cout;
using std::endl;
ExtPenStyle xps = ExtPenStyle();
unsigned long ulong(static_cast<unsigned long>(xps));// no good?!?
cout << "Sizeof ExtPenStyle: " << sizeof(ExtPenStyle) << endl;
return 0;
}
brain (no comments, please<g>). Anyway, I have the following struct
containing bit fields:
struct ExtPenStyle
{
unsigned long style : 4;
unsigned long : 4; // unused
unsigned long endcap : 2;
unsigned long : 2; // unused
unsigned long join : 2;
unsigned long : 2; // unused
unsigned long type : 1;
unsigned long : 15; // unused
};
I tried casting from/to unsigned long using static_cast,
reinterpret_cast and C-style cast. The compiler refuses to do it, but
I need to call API code which expects an unsigned long with bitmapped
values.
I know I could use a union or memcpy(), but I wonder if there is a
cleaner way? Thanks.
#include <iostream>
#include <ostream>
// struct ExtPenStyle
// { see above };
int main()
{
using std::cout;
using std::endl;
ExtPenStyle xps = ExtPenStyle();
unsigned long ulong(static_cast<unsigned long>(xps));// no good?!?
cout << "Sizeof ExtPenStyle: " << sizeof(ExtPenStyle) << endl;
return 0;
}