Home > Backend Development > C++ > C++ compilation error: non-const objects cannot call const member functions, how to solve it?

C++ compilation error: non-const objects cannot call const member functions, how to solve it?

PHPz
Release: 2023-08-21 21:33:06
Original
1300 people have browsed it

C Compilation error: Non-const objects cannot call const member functions. How to solve it?

In C, const member function refers to adding the const keyword after the function declaration, which means that the function will not modify the state of the object. When an object is of const type, ordinary non-const member functions cannot be called, but const member functions can be called. However, when a non-const object calls a const member function, the following error will occur during compilation: Non-const objects cannot call const member functions.

This error usually occurs in the following two situations:

  1. A non-const object calls a const member function

An example:

class Example {
public:
  void doSomething() const {
    // const function
  }
};

int main() {
  Example obj;
  obj.doSomething(); // 编译错误
  return 0;
}
Copy after login

When calling a const member function on a non-const object, the compiler will give the above error. This is because the object's state may be changed by functions, and the compiler has no way of knowing whether a const member function really doesn't modify the object.

Solution:

To avoid this error, you need to mark the calling object as const. This can be achieved by adding the const keyword after the object type, for example:

const Example obj;
obj.doSomething(); // 正确,因为 obj 是 const 类型的
Copy after login
  1. const object calls non-const member function

For example:

class Example {
public:
  void doSomething() {
    // non-const function
  }
};

int main() {
  const Example obj;
  obj.doSomething(); // 编译错误
  return 0;
}
Copy after login

In this case, because the object is of const type, the compiler will think that the state of the object cannot be modified, and thus refuse to call non-const member functions.

Solution:

If you really need to modify the state of the object, you can convert it to a non-const type. This can be achieved by using the type conversion operator const_cast, for example:

const Example obj;
const_cast<Example&>(obj).doSomething(); // 正确,但不建议这样做
Copy after login

It should be noted that using const_cast for type conversion may lead to undefined behavior, so its use in formal code should be avoided.

Summary

The above are the solutions to two common C compilation errors: non-const objects cannot call const member functions. When writing code, you should take care to mark const member functions as const and avoid calling non-const member functions on const objects. If you really need to modify the state of a const object, you should use const_cast for type conversion, but you should avoid using it in formal code.

The above is the detailed content of C++ compilation error: non-const objects cannot call const member functions, how to solve it?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template