一个奇怪的 C++ 错误:test.cpp:15: 错误:将‘const *’作为‘*’的‘this’参数丢弃限定符

2021-12-31 00:00:00 compiler-errors constants c++

我在处理一段特定的代码时遇到了一些问题,如果有人能在这件事上启发我,我将不胜感激,我已在以下示例中隔离了该问题:

I'm having some trouble with a particular piece of code, if anyone can enlighten me on this matter it would be greatly appreciated, I've isolated the problem down in the following sample:

#include <iostream>

using namespace std;

class testing{
   int test();
   int test1(const testing& test2);
};

int testing::test(){
   return 1;
}

int testing::test1(const testing& test2){
   test2.test();
   return 1;
}

那么可能导致以下错误的原因是什么:

So what could possibly have cause the following error:

test.cpp:15: 错误:将const testing"作为int testing::test()"的this"参数传递会丢弃限定符

test.cpp:15: error: passing ‘const testing’ as ‘this’ argument of ‘int testing::test()’ discards qualifiers

非常感谢!

推荐答案

问题是在 上调用非 const 函数 test2.test()const 对象 test2 来自 testing::test1.

The problem is calling a non-const function test2.test() on a const object test2 from testing::test1.

testing::test1 获取 test2 作为参数 const testing &test2.所以在 testing::test1 中,test2const.然后在函数的第一行:

testing::test1 gets test2 as a parameter const testing &test2. So within testing::test1, test2const. Then in the first line of the function:

test2.test()

testing::test 函数在 test2 上被调用.该函数未在签名末尾使用 const 声明,因此它可能会修改它被调用的对象(隐式传递给它的 this 指针),即使它不,编译器假定如此.通过让您在那里调用它,编译器将允许您修改 const 变量而无需显式转换,而 C++ 不应该允许这种情况.因此解释错误信息:

The testing::test function is called on test2. That function is not declared with const at the signature end, so it may modify the object it is called on (the this pointer implicitly passed to it), and even though it does not, the compiler assumes so. By letting you call it there, the compiler would let you modify a const variable without an explicit cast, which C++ is not supposed to allow. Therefore to explain the error message:

test.cpp:15: error: passing ‘const testing’ as ‘this’ argument of ‘int testing::test()’ discards qualifiers

this 指的是成员函数 (testing::test) 操作的对象,在这种情况下它不是 const,因为 testing::test 没有用 const 声明,因此当试图创建一个非 const 指针(const>this) 引用一个 const 对象 (testing),忽略 const qualifier.

this refers to the object the member function (testing::test) operates on, and in this case it is not const, because testing::test was not declared with const, and thus the mismatch is detected when trying to make a non-const pointer (this) refer to a const object (testing), ignoring the const qualifier.

为了解决这个问题,决定 testing::test 函数是否需要修改它被调用的对象(现在它的编写方式不需要,因为它所做的只是 return 1,但是这可能会改变,所以你需要考虑它的预期功能是什么).如果应该,那么显然在 const 对象上调用它是不好的,尽管您可以使用 const_cast 要求编译器覆盖它,但这是危险的.如果不应该,则将其标记为 const,以便它也可以在 const 对象上调用:

To solve this, decide whether the testing::test function should ever need to modify the object it is called on (the way it is written now it does not, as all it does is return 1, however that may change, so you need to think at what its intended functionality is). If it should, then obviously calling it on a const object is bad, although you can use const_cast to ask the compiler to override that, but this is dangerous. If it should not, then mark it const, so that it can be called on const objects as well:

class testing{
    int test1() const;
    // ...
}

int testing::test() const {
    // ...
}

相关文章