错误:将'const …'作为'…'的参数传递'this'在调用方法中丢弃限定符
error: passing 'const …' as 'this' argument of '…' discards qualifiers in calling method
我正在将对象的引用传递给一个函数,我使用 const 来指示它是只读方法,但是如果我在该方法中调用另一个方法,即使我没有将引用作为参数传递,也会发生此错误。
错误:将"const A"作为"void A::hello()"的"this"参数传递会丢弃限定符 [-permissive]
错误:将"const A"作为"void A::world()"的"this"参数传递会丢弃限定符 [-fpermissive]
#include <iostream>
class A
{
public:
void sayhi() const
{
hello();
world();
}
void hello()
{
std::cout << "world" << std::endl;
}
void world()
{
std::cout << "world" << std::endl;
}
};
class B
{
public:
void receive(const A& a) {
a.sayhi();
}
};
class C
{
public:
void receive(const A& a) {
B b;
b.receive(a);
}
};
int main(int argc, char ** argv)
{
A a;
C c;
c.receive(a);
return 0;
}
既然sayhi()
是const
,那么它调用的所有函数也必须声明为const
,在本例中为hello()
和world()
。您的编译器正在警告您有关恒定正确性的信息。
相关文章:
- 如何强制从重写方法调用重写的方法基方法?
- C++:使用方法调用析构函数的顺序是什么?
- 派生类调用父类的方法,该方法调用重写的虚拟方法调用错误的方法
- 使用 object 中的方法调用带有 std::bind 和 std::function.target 的 C 样式函数
- 指向类方法调用的指针
- 如何使用 SFINAE 在方法调用中有条件地定义变量?
- 是否有可以处理方法调用依赖关系的设计模式?
- 如何缩短C++中的方法调用?
- 从部分专用模板方法调用模板非静态方法
- 有没有办法禁止派生类中的基类方法调用?
- 为什么这C++只在编译器上编码一个不明确的方法调用Microsoft?
- 从父方法调用子方法
- 如何将子方法调用到父方法
- 虚拟函数在哪里使用 vpointer to vtable 来解析方法调用,非虚拟方法存储在哪里以及如何解析它们?
- 从静态方法调用静态函数指针
- 从同一类中的另一个方法调用方法时出错
- 方法调用意外地像 l 值一样起作用
- 无法从派生的一个方法调用基类方法
- 从类方法调用命名空间中名为 Same 的函数时,重载解析失败
- C 多线程JAVA JNI方法调用