c++类型转换问题:和atl相关

vc21888 2005-03-11 11:38:32
#include <windows.h>
#include <iostream>
using namespace std;

class Base1 {
public:
virtual void f() { }
};

class Base2 {
public:
virtual void f() { }
};

class Base3 {
public:
virtual void f() { }
};

class Drive : public Base1, public Base2, public Base3 {
};

#define _ATL_PACKING 8

#define offsetofclass(base, derived) \
((DWORD)(static_cast<base*>((derived*)_ATL_PACKING))-_ATL_PACKING)

int main() {
cout << offsetofclass(Base1, Drive) << endl;
cout << offsetofclass(Base2, Drive) << endl;
cout << offsetofclass(Base3, Drive) << endl;
return 0;
}

程序的输出为:
0
4
8

在宏offsetofclass当中,((derived*)_ATL_PACKING)的值仍然是_ATL_PACKING,
为什么加上(static_cast<base*>((derived)_ATL_PACKING)))之后结果发生了变化,
请那为高手帮忙解答一下;;;感谢;;;
...全文
89 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
neilxp 2005-03-12
  • 打赏
  • 举报
回复
up
masterz 2005-03-12
  • 打赏
  • 举报
回复
The dynamic_cast and static_cast operators move a pointer throughout a class hierarchy.
这里关心的是相对值,_ATL_PACKING绝对值得变化没什么关系,只要不为0就行了

3,245

社区成员

发帖
与我相关
我的任务
社区描述
ATL,Active Template Library活动(动态)模板库,是一种微软程序库,支持利用C++语言编写ASP代码以及其它ActiveX程序。
社区管理员
  • ATL/ActiveX/COM社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧