105

Today I got a problem. I am in the need of a static member function, const is not a must but a better. But, I didn't succeed in my efforts. Can anybody say why or how?

msc
  • 33,420
  • 29
  • 119
  • 214
prabhakaran
  • 5,126
  • 17
  • 71
  • 107

5 Answers5

171

When you apply the const qualifier to a nonstatic member function, it affects the this pointer. For a const-qualified member function of class C, the this pointer is of type C const*, whereas for a member function that is not const-qualified, the this pointer is of type C*.

A static member function does not have a this pointer (such a function is not called on a particular instance of a class), so const qualification of a static member function doesn't make any sense.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • Does it mean that 'const' is only for variables like int,pointer,etc.? – prabhakaran Aug 12 '11 at 04:45
  • @prabhakaran - Member functions can be **const** qualified too. James clearly mentioned it in his answer. – Mahesh Aug 12 '11 at 04:53
  • @Mahesh Member function's const qualifier also, qualifies only the 'this pointer', not the whole. You can still change the global variable within the const qualified member function. Now only I checked this. – prabhakaran Aug 12 '11 at 09:18
  • I forgot to add this line at starting of the above "After practically checked Steve Jessop's(below) comment I have to say that" – prabhakaran Feb 04 '14 at 14:53
  • 8
    **-1** "A static member function does not have a this pointer ... so const qualification of a static member function doesn't make any sense [that way]" is true in itself but is wrong as an anwer to the question's "why". As an answer it presumes that the meaning of `const` would have to be the same for a static member as for a non-static one. As an example that that thinking doesn't hold, consider the meaning of `static`, which depends on the context. – Cheers and hth. - Alf Dec 29 '14 at 14:04
  • 3
    Is it `C const*` or `const C*` ? – crisron Jun 25 '16 at 06:54
  • Shouldn't a const qualifier on member function makes "this" be of type "C const *const", since "this" by default is a constant pointer? – Cheshar Nov 27 '18 at 06:29
  • 1
    @crisron both are identical. In C/C++, whether or not you put the `const` qualifier for the innermost type on the left or right has no effect. – Tyg13 Mar 29 '19 at 14:02
  • What if you want it to promise not to change any of the static members? – SMeznaric Jul 07 '21 at 11:42
  • Both are identical, because `const` acts on what is on his LEFT. And if there is nothing on his left, acts on the first element on his Right. It's a common habit to write `const C*`, and it's OK. But the interpreted syntax is `C const *`. – Sandburg Sep 14 '22 at 09:56
30

I agree with your question, but unfortunately the C++ is designed that way. For example:

class A {
  int i;         //<--- accessed with 'this'
  static int s;  //<---- accessed without 'this'
public:
  static void foo ()  const // <-- imaginary const
  {}
};

As of today, the const is considered in context of this. In a way, it's narrow. It can be made broader by applying this const beyond this pointer.
i.e. the "proposed" const, which may also apply to static functions, will restrict the static members from any modification.

In the example code, if foo() can be made const, then in that function, A::s cannot be modified. I can't see any language side effects, if this rule is added to standard. On the contrary, it's amusing that why such rule doesn't exist!

iammilind
  • 68,093
  • 33
  • 169
  • 336
  • 9
    Probably the same as the reason that you can't const-modify a free function to mean "this function doesn't modify any global variables". `const` applies to an object (in the case of const member functions, the instance on which it's called). You want to make it apply to all static members of the class, I would guess that if it was considered at all by the committee, then it wasn't thought to be a common enough requirement to support. – Steve Jessop Aug 12 '11 at 08:34
  • 1
    The `const` modifier isn't applied to member methods or member variables, but to the implicit `this`-pointer. Since a static member method is not bound to an object there is no `this`-pointer to make `const`. – Ruud Althuizen Jan 04 '17 at 14:14
1

It is unfortunate that C++ doesn't accept it as per design but logically there are few use cases in which it validates well.

A function which is class level valid(static) might not change any static data, may be it will just query data should be const. May be it should be like

if(Object)
    MakeThisConstant()
else
    MakeStaticDataConstant() // Only in the scope but static data cannot be constant so may be it should in some scenarios
0

Without getting into the details, it's because there may or may not be an object modified by the function, so const is ambiguous to the compiler.

Recall that const keeps objects constant, but there may or may not be an object here to keep constant.

Don Larynx
  • 685
  • 5
  • 15
  • 2
    "may or may not"? A static member function *never* has a `this` pointer. (Besides which, `const` doesn't keep objects constant. It prevents a particular pointer or reference from being used to modify the object, but modification might still happen through another path) – Ben Voigt Apr 05 '15 at 20:04
0

A 'const member function' is not allowed to modify the object it is called on, but static member functions are not called on any object. It is used directly by scope resolution operator. Thus having a const static member function makes no sense, hence it is illegal.

  • static const have a good sense as just a scope binding to the class where it is defined, or to provide getter for global private fields. – Netch Jan 24 '23 at 08:19