CONST enhancement of class object member functions

zhaozj2021-02-08  429

The Const Enhancement of the Class Object member function's Const Enhancement C language version 2.0 is configured for several new applications, in the early versions of C , the call of the member function may change the value of the Const class object. This is definitely a problem. If we don't allow the members of the Const class, we have to use the Const object to dry, in order to make language become complete, introduce the Const member function after version 2.0, which is the member function that allows the Const object to be called. The compiler guarantees that the Const member function does not change the value of the object internal data used in the member function, but also guarantees the object of the Const class to use the const type of functions. Let's take a look at the code: struct s {

INT A; F (int AA) {RETURN A = aa;} g (int AA) const {return AA;} // Please note that this function does not change the value of A as class member data (INT AA) Const {RETURN A = aa;} // This sentence is wrong because it tries to change the value of the member data A of the class.

}; void g () {

S O1; Const S O2; // Please note that this is an object of a const type; O1.A = 1; // O2.a = 2; O1.f (3); // O2.f (4) // Try to call a member function of the non-Const type in the Const type object; O1.G (5); O2.G (6);

} If we recover some of the statements that comments falling, we will find that such a program cannot be compiled. Because they are not trying to call non-const type functions in the const type object, they are trying to change the value of the class member data in the const type member function.

转载请注明原文地址:https://www.9cbs.com/read-841.html

New Post(0)