dynamic_cast在做downcast的时候出了问题.
问题:
ChdProcWinDlg *pParent = dynamic_cast<ChdProcWinDlg*>(pwnd->GetParent());
有错吗?
为什么我都得到null
直接强转就ok
ChdProcWinDlg *pParent = (ChdProcWinDlg*)pwnd->GetParent();
(运行期正确)
环境:
这个是在线程里面, 需要使用到窗口类中的方法
pwnd 是通过 DWORD WINAPI ThreadFunc(LPVOID)中传过去的参数用reinterpret_cast转换得到:
CDlgPC * pwnd = reinterpret_cast<CDlgPC*>(lpParam);
CDlgPC是ChdProcWinDlg的子窗口
牢骚:
CWnd => ChdProcWinDlg 是一个 downcast啊~
难道是我对dynamic_cast理解有偏差?
还是GetParent();给我的不是CWnd*
昏了~~
附加:
同样的问题, 出在下面:
(1)
CWnd *pwnd = CWnd::FromHandle(GlobalVar.hwnd_main);
Dlg_Main *pdlg = DYNAMIC_DOWNCAST(Dlg_Main, pwnd);
运行期正确
(环境: 用在dll的函数里面, 需要修改到hwnd_main这个窗口内的一些数据)
(2)
Dlg_Main *pdlg = DYNAMIC_DOWNCAST(Dlg_Main, GetParent());
运行期正确
(环境: 用在Dlg_Main子窗口内)
(3)
ChdProcWinDlg *pParent = DYNAMIC_DOWNCAST(ChdProcWinDlg, pwnd->GetParent());
运行期错误(NULL)
(环境同最上面的一样)
开发工具: VC.NET 2003
ps:不知道该po在哪边, 属于MFC还是C++的问题?
ChdProcWinDlg *pParent = dynamic_cast<ChdProcWinDlg*>(pwnd->GetParent());
有错吗?
为什么我都得到null
直接强转就ok
ChdProcWinDlg *pParent = (ChdProcWinDlg*)pwnd->GetParent();
(运行期正确)
环境:
这个是在线程里面, 需要使用到窗口类中的方法
pwnd 是通过 DWORD WINAPI ThreadFunc(LPVOID)中传过去的参数用reinterpret_cast转换得到:
CDlgPC * pwnd = reinterpret_cast<CDlgPC*>(lpParam);
CDlgPC是ChdProcWinDlg的子窗口
牢骚:
CWnd => ChdProcWinDlg 是一个 downcast啊~
难道是我对dynamic_cast理解有偏差?
还是GetParent();给我的不是CWnd*
昏了~~
附加:
同样的问题, 出在下面:
(1)
CWnd *pwnd = CWnd::FromHandle(GlobalVar.hwnd_main);
Dlg_Main *pdlg = DYNAMIC_DOWNCAST(Dlg_Main, pwnd);
运行期正确
(环境: 用在dll的函数里面, 需要修改到hwnd_main这个窗口内的一些数据)
(2)
Dlg_Main *pdlg = DYNAMIC_DOWNCAST(Dlg_Main, GetParent());
运行期正确
(环境: 用在Dlg_Main子窗口内)
(3)
ChdProcWinDlg *pParent = DYNAMIC_DOWNCAST(ChdProcWinDlg, pwnd->GetParent());
运行期错误(NULL)
(环境同最上面的一样)
开发工具: VC.NET 2003
ps:不知道该po在哪边, 属于MFC还是C++的问题?
...全文
相关推荐
static_cast,dynamic_cast,reinterpret_cast,const_cast的区别及用法详解
静态转换(static_cast)是C++中的类型转换方式之一,主要用于在编译时进行明确且安全的类型转换。它的主要特点是:1. 对于类指针,static_cast仅限于将指向具有继承关系的对象的指
static_cast、dynamic_cast、reinterpret_cast和const_cast
标准C++的类型转换符:static_cast、dynamic_cast、reinterpret_cast和const_cast
static_cast,dynamic_cast,reinterpret_cast和const_cast的区别详解
"C++中的类型转换机制包括四种标准转换方式:static_cast、dynamic_cast、reinterpret_cast和const_cast。这些转换符提供了比C-style cast更为精
C++中的类型转换static_cast、dynamic_cast、const_cast和reinterpret_cast总结
本文将详细介绍四种主要的显式类型转换:`static_cast`、`dynamic_cast`、`const_cast`和`reinterpret_cast`。
标准C++的类型转换符:static_cast、dynamic_cast、reinterpret_cast和const_cast
### 标准C++的类型转换符:static_cast、dynamic_cast、reinterpret_cast和const_cast在C++编程语言中,为了提高程序的可读性和安全性,C++标准库提供了多种类型的转换机制


非技术区
