当它不是时,C++ 将其视为 const

标签 c++ visual-c++ constants this

<分区>

我正在尝试使用 Visual Studio 2010 编译一些 C++ 代码,但出现以下错误:

error C2664: 'molder::Mold::set_piece_maker' : cannot convert parameter 1 from 'piece_maker::Piece_Maker *const ' to 'piecemaker::Piece_Maker *'

该错误指的是管理两个类之间相互引用的两个镜像函数:

void Piece_Maker::set_mold(molder::Mold* value, void* origin) {
    if (this->mold == value)
        return;

    this->mold = value;
    this->mold->set_piece_maker(this, this); // This is the line with the error
}

和:

void Mold::set_piece_maker(piecemaker::Piece_Maker* value, void* origin) {
    if (this->piece_maker == value)
        return;

    this->piece_maker = value;
    this->piece_maker->set_mold(this, this);
}
  • 我没有在任何地方对这些类中的任何一个使用 const。
  • 错误只发生在 mold->set_piece_maker() 行,而镜像的 piece_maker->set_mold() 行编译得很好。
  • 编译器声称“this”是 const,但没有标记我对其 mold 属性的修改,也没有标记我传递的“this”作为 origin 参数。
  • 当我实际将 set_mold() 设置为常量时,编译器会在尝试修改该函数中的模具并尝试将“this”作为原点传递时引发错误。

编译器引发该错误可能会发生什么?

最佳答案

问题不在于 const,它是顶级 const,无论如何都会被忽略。看一下垂直排列的两种类型:

piece_maker::Piece_Maker *const
piecemaker::Piece_Maker *

关于当它不是时,C++ 将其视为 const,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27772129/

相关文章:

c++ - 强制外部函数为 const

c++ - 立方体在 OpenGL 中的边缘

c++ - 在 Visual Studio C++ 中创建应用程序图标的最佳方式

visual-c++ - 安装 Visual Studio 2015 后找不到 C++ 项目模板

C++ delete 不释放所有内存 (Windows)

PHP - 定义常量与使用 config.ini

c++ - 按位比较效率

Android NDK C++ openGL ES 2 上下文显示效果不佳

c++ - 从OpenCV的视频捕获中仅读取mp4文件的一部分

用于存储常量的 Android 嵌套接口(interface)