Home >Backend Development >C++ >C++ syntax error: const-modified member functions must declare const members, how to deal with it?

C++ syntax error: const-modified member functions must declare const members, how to deal with it?

王林
王林Original
2023-08-22 13:51:361198browse

C++ syntax error: const-modified member functions must declare const members, how to deal with it?

C Syntax error: A const-modified member function must declare a const member. How to deal with it?

In C language, const is a very important keyword, which is used to modify certain variables, pointers, member functions, etc. For member functions, if it is modified with the const keyword, the value of the member variable cannot be modified inside the function body. However, if we do not add the const keyword in both the function declaration and definition, we will encounter a compilation error "const-modified member functions must declare const members." So how do we deal with this problem?

The way to solve this problem is very simple, just add the const keyword after the function declaration and definition parameter list. For example, we have a class named "Test", which has a member function "getValue()", which returns a value of type int. If we want to ensure the immutability of member variables within this function, we can declare it as a const member function. As shown below:

class Test {
public:
    int getValue() const;  // 声明const成员函数
private:
    int m_value;
};

int Test::getValue() const {  // 定义const成员函数
    return m_value;
}

As you can see, the const keyword needs to be added after the parameter list when declaring and defining functions. At this time, the compiler will know that this function is a const member function, thus prohibiting modification of member variables within the function body.

It should be noted that if a member function is declared as a const member function, the value of the member variable cannot be modified internally, but the value of the member variable can be read. At the same time, const member functions cannot call non-const member functions, because this may cause the value of the member variable to be modified.

In short, in C, we can use the const keyword to modify member functions to ensure the immutability of member variables. If you encounter a compilation error "const-modified member functions must declare const members" when declaring and defining functions, you only need to add the const keyword after the parameter list to solve the problem.

The above is the detailed content of C++ syntax error: const-modified member functions must declare const members, how to deal with it?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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