空型铸造到类型
Void cast to type
我有基于MFC对话框的应用程序。
void CThr_MfcDlg::OnBnClickedButton1()
{
this->SetWindowTextW(L"bla");
(CThr_MfcDlg*)GetDlgItem(IDD_THR_MFC_DIALOG)->SetWindowText(L"hello") ;
}
第this->SetWindowTextW(L"bla");
行将标题更改为bla
我预计第(CThr_MfcDlg*)GetDlgItem(IDD_THR_MFC_DIALOG)->SetWindowText(L"hello") ;
行应该将标题更改为hello
,但出现编译错误:
Error 1 error C2440: 'type cast' : cannot convert from 'void' to 'CThr_MfcDlg *'
阅读本文。由于->运算符的优先级(2)高于强制转换运算符的优先级
(CThr_MfcDlg*) (GetDlgItem(IDD_THR_MFC_DIALOG)->SetWindowText(L"hello")) ;
为了避免这种情况,应该在强制转换时使用括号。
// this will be correct.
((CThr_MfcDlg*)GetDlgItem(IDD_THR_MFC_DIALOG))->SetWindowText(L"hello");
相关文章:
- 为什么会出现 gettnig 运行时错误:加载类型为"_Bit_type"(stl_bvector.h) 的空指针?
- 表示类模板C++空类型
- 给定一个类型,如何派生一个泛型更广泛的类型(例如,用于溢出安全求和)?
- 运行时错误:引用绑定到类型为"int"的空指针
- 我在 C++ 代码中遇到错误警告:控制到达非空函数 [-Wreturn 类型] 的末尾
- 将空*参数转换为各种类型的参数是UB吗?
- 赋值到类型和空大括号. 语法说明
- 泛型枚举和其他类型的重载模板函数
- 使用泛型类型推送到堆栈时出现问题
- 为什么在传递长整型时调用具有两个双精度类型的参数的重载函数?
- 以特征类型作为参数的泛型函数回调
- 将整型常量映射到类型
- 将-Wtype限制与类型泛型代码一起使用
- 从函数中存储和返回泛型类型(甚至为空)
- 优选的返回类型:空或布尔
- 为什么C类型泛型表达式不能与C++兼容
- 如何在 Java 中模拟'question mark(?)'类型泛型以C++?
- 空型铸造到类型
- 构造函数不允许返回类型(泛型链表)
- 如何构建一个泛型方法,将不同Q_PROPERTY类型的 notifySignal 从属性 char * 名称连接到空槽