c++ - 为什么指针没有隐式转换成常量*常量
问题描述
AT45Status
是一个有几个成员的结构,我有以下功能:
/**
* @brief read the chip status from the flash chip
* @retval a pointer to the current status of the chip
*/
AT45Status const * const ReadChipStatus(void) {
uint8_t commands[1] = {STATUS_READ};
static AT45Status status;
//// … details, but the result is updated values inside status
return &status;
}
如果我使用 C++ 编译它,我会收到以下警告:
...(16):警告:#815-D:返回类型的类型限定符没有意义
我当然查找了一个确切的解释,这是对语义传递的普通类型的相关警告。但我正在返回一个指向我不想更改的对象的指针。(指针或指向的数据)。因此,类型限定符是有意义的。
当我将 return 语句更改为:
return (AT45Status const * const)&status;
为什么编译器不能/不将 an 隐式AT45Status*
转换为 an AT45Status const * const
?我在这次互动中缺少什么?
解决方案
该警告试图告诉您您正在尝试返回一个const
指针,这是没有意义的,因为返回的指针本身无法修改。
另一方面,将被指向的对象限定为const
有意义(使指向的对象不可修改),即返回指向const
. 例如
AT45Status const * ReadChipStatus(void)
推荐阅读
- c++ - C++,在多行代码中注释
- node.js - 如何在 Angular 中选中多个复选框,其中复选框值位于带有 csv 的数据库的一列中?
- java - 在 Android 手机上部署应用程序时找不到 java.time.LocalDateTime 的类
- imagemagick - 提取/转换转换后的(多文件)图像
- ios - 为什么 UITableView 在重新加载数据时会缓存单元格?
- python - python 中有哪些可用的模块,例如 http 和 idlelib?
- android - 无法使用 Firebase 数据库在导航标题中显示配置文件图像
- python - odeint 方程组的动态生成
- java - 从 jpanel 中删除组件时出现 NullPointerException
- python - 使用日期时间过滤 json 结果 - 排除周末 - Python