Why can a const member function modify a static data item?

In the following C++ program, changing the function of the static data element from the const function works fine:

 class A { public: static int a; // static data member void set() const { a = 10; } }; 

But changing a non-static data element from const function does not work:

 class A { public: int a; // non-static data member void set() const { a = 10; } }; 

Why does the const member function change the static data member?

+76
c ++ function static c ++ 11 const
May 12 '17 at 11:11
source share
4 answers

This is the rule, that's all. And not in vain.

The const qualifier for a member function means that you cannot modify member variables of the mutable class not static .

As a suggestion for some rationalization, the this pointer in const qualified member function is of type const , and this inherently associated with an instance of the class. Stat members are not associated with an instance of the class. You do not need an instance to change the static member: you can do this in your case by writing A::a = 10; .

So, in your first case, think about a = 10; as an abbreviation for A::a = 10; , and in the second case, think of it as a shorthand for this->a = 10; , which does not compile, since the type this is equal to const A* .

+91
May 12 '17 at 11:14
source share

According to the C ++ standard (9.2.3.2 static data elements)

1 Static data member is not part of class subobjects ...

And (9.2.2.1 This pointer)

1 In the body of a non-static (9.2.1) member function, the keyword is the prvalue expression whose value is the address of the object for which the function is called. The type of this member in a function of class X is X *. If a const member function is declared, the type of it is const X * , ...

Finally (9.2.2 Non-Static Member Functions)

3 ... if the name search (3.4) resolves the name in the id expression to a non-static non-type member of some class C, and if either the id expression is potentially evaluated or C is X or the base class X, the id expression is converted to an access expression class member (5.2.5), using (* this) (9.2.2.1) as the postfix expression to the left of. Operator.

So in this class definition

 class A { public: static int a; void set() const { a = 10; } }; 

the static data member a not a subobject of an object of class type, and the this pointer is not used to access the static data element. Thus, any member function, a non-static constant or not a constant, or a static member function can modify a data item because it is not a constant.

In this class definition

 class A { public: int a; void set() const { a = 10; } }; 

the non-static data member a is a subobject of an object of type class. To access it in a member function, either the syntax for accessing membership in this syntax is used. You cannot use the this constant pointer to change a data item. And the pointer really has type const A * inside the set function, because the function is declared using the const qualifier. If the function did not have a qualifier, in this case the data item could be changed.

+21
May 12 '17 at 11:51
source share

The fact is that if a member function of class A is const , then this is equal to const X* and thereby prevents the change of non-static data members (cf, for For example, C ++ standard ):

9.3.2 This pointer [class.this]

In the body of a non-static (9.3) member function, the key word is the expression prvalue, value is the address of the object for which the function is called. The type of this in a member function of class X is X *. If a function member is declared const, whose type is const X *, ...

If A is a non-static data member, then a=10 matches this->a = 10 , which is unacceptable if the type this is const A* and A not been declared mutable . Thus, since void set() const makes the type this const A* , this access is not allowed.

If A is a static data element, then, on the contrary, a=10 does not include this at all; and until static int a itself was declared as const , the a=10 operator is allowed.

+13
May 12 '17 at 11:32
source share

The const qualifier in the member function means that you cannot change the non-mutable , non-static members of the class data .

+1
May 15 '17 at 5:57 a.m.
source share



All Articles