首页 > 解决方案 > 为什么指针没有隐式转换成常量*常量

问题描述

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?我在这次互动中缺少什么?

标签: c++pointersconstantskeil

解决方案


该警告试图告诉您您正在尝试返回一个const指针,这是没有意义的,因为返回的指针本身无法修改。

另一方面,将被指向的对象限定为const有意义(使指向的对象不可修改),即返回指向const. 例如

AT45Status const * ReadChipStatus(void)

推荐阅读