首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

union类中使用位域,这段数据成员有什么有关问题吗?内存地址不对

2013-04-21 
union类中使用位域,这段数据成员有什么问题吗?内存地址不对union _stringtime_data{typedef std::string::

union类中使用位域,这段数据成员有什么问题吗?内存地址不对
union _stringtime_data
{
typedef std::string::value_type char_t;
typedef std::string::pointer pointer;
typedef std::string::const_pointer const_pointer;
char_t _mTimeData[8];
char_t year :4,month :2,day :2;
}
我对于这个结构体是这么理解的
_mTimeData有8个char_t,
year占_mTimeData里的4个,在最前面,
month占2个,在year的后面,day在month后面,占2个
也就是说取year的地址其实就是_mTimeData的地址
但是我在使用的时候却不是这个样子,
year的起始地址和_mTimeData不一样..
[解决办法]
union _stringtime_data
{
typedef std::string::value_type char_t;
typedef std::string::pointer pointer;
typedef std::string::const_pointer const_pointer;

char_t _mTimeData[8];
struct {
   char_t year :4,month :2,day :2;
} _m2;
};

这样才是你想要的那种.

热点排行