使用DYNAMIC_DOWNCAST转换后指针为空,强制类型转换就可以???

mltong 2011-02-22 11:14:34
IDC_BUTTON_TEST是对话框上一个button控件的ID。

使用DYNAMIC_DOWNCAST:


使用强制类型转换:
...全文
498 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
还是不明白啊,为啥IsKindOf返回不是TRUE
难道强制转成CButton类型后还只有CWnd的功能?
Eleven 2011-02-22
  • 打赏
  • 举报
回复
[code=C]/C++CObject* AFX_CDECL AfxDynamicDownCast(CRuntimeClass* pClass, CObject* pObject)
{
if (pObject != NULL && pObject->IsKindOf(pClass))
return pObject;
else
return NULL;
}[/code]
mltong 2011-02-22
  • 打赏
  • 举报
回复
CWnd和CButton都是MFC类啊,它们声明的有吧
[code=C]/C++
class CWnd : public CCmdTarget
{
DECLARE_DYNCREATE(CWnd)
protected:
static const MSG* PASCAL GetCurrentMessage();

// Attributes
public[/code]
[code=C]/C++class CButton : public CWnd
{
DECLARE_DYNAMIC(CButton)

// Constructors
public:
CButton();
BOOL Create(LPCTSTR lpszCaption, DWORD dwStyle,
const RECT& rect, CWnd* pParentWnd, UINT nID);
[/code]
[Quote=引用 1 楼 year2002 的回复:]

DECLARE_DYNAMIC声明了?
[/Quote]
手机写程序 2011-02-22
  • 打赏
  • 举报
回复
好高的结贴率。。。
派生的问题,DYNAMIC_DOWNCAST(CWnd,...)这样没有问题。
Eleven 2011-02-22
  • 打赏
  • 举报
回复
The macro will cast the pointer parameter to a pointer to an object of the class parameter's type.

If the object referenced by the pointer is a "kind of" the identified class, the macro returns the appropriate pointer. If it is not a legal cast, the macro returns NULL.
  • 打赏
  • 举报
回复
DECLARE_DYNAMIC声明了?
mltong 2011-02-22
  • 打赏
  • 举报
回复
csdn的代码格式化有问题吧,代码怎么这样。。。
mltong 2011-02-22
  • 打赏
  • 举报
回复
这样试了试,是可以转换的
[code=C]/C++ CButton *pButton = new CButton;
CWnd *pWnd = pButton;
pWnd = DYNAMIC_DOWNCAST(CButton, pWnd);[/code][Quote=引用 7 楼 hdt 的回复:]

C++ 在运行时,是没有面向对象的概念了,不同于 .net ,mfc 是通过 cruntimeclass 来模拟运行时的类型信息

getdlgitem 只是通过 id 号找到 一个窗口或者控件的handler ,再把这个handle 赋给 cwnd
所以他不带有cbutton cruntimeclass 信息 .
那个宏 我估计是为了
cbutton* button = new b……
[/Quote]
真相重于对错 2011-02-22
  • 打赏
  • 举报
回复
C++ 在运行时,是没有面向对象的概念了,不同于 .net ,mfc 是通过 cruntimeclass 来模拟运行时的类型信息

getdlgitem 只是通过 id 号找到 一个窗口或者控件的handler ,再把这个handle 赋给 cwnd
所以他不带有cbutton cruntimeclass 信息 .
那个宏 我估计是为了
cbutton* button = new button
CWnd* pwnd = button;
....

DYNAMIC_DOWNCAST(CButton,pwnd);

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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