2

Take this example:

SomeClass.h

class Foo {
public:
    static int bar;
    int x;
    void someFunc() {
        this->x = 5;
        this->bar = 9;
    }
};

SomeClass.cpp

int Foo::bar = 0;

mainc.pp

#include <iostream>
#include "SomeClass.h"

int main() {
    Foo f;
    f.someFunc();

    std::cout << "f.x = " << f.x << '\n';
    std::cout << "f.bar = " << f.bar << '\n';
    return 0;
}

Compiled and built with Visual Studio 2017CE.

Output

f.x = 5
f.bar = 9

However according to cppreference:static

Static members of a class are not associated with the objects of the class: they are independent variables with static or thread (since C++11) storage duration or regular functions.

Now as for static member functions they state:

Static member functions are not associated with any object. When called, they have no this pointer.

I just want some clarity on this: I had thought that both static members and static function members did not have the this pointer associated with them...

Francis Cugler
  • 7,788
  • 2
  • 28
  • 59
  • 1
    Imagine you have a static value owned by a class sitting in the nether region of memory. The "this" pointer is just that current object. The "this" pointer of any object can access that static member but it's only in one place in memory so all objects share it. You are getting the wording confused. It never says you can't use a pointer to access the static value! – Bayleef Jun 26 '19 at 00:35
  • @BaileyKocin thank you for the clarification. So when they say that static members have no association to an object and that they can not be accessed by the `this pointer`, they are referring specifically to `static functions` of that class then. – Francis Cugler Jun 26 '19 at 00:37
  • 1
    The static functions of a class have no internal this ponter. That is correct. However you can use the "this" pointer to access the static member function in the class because again its just a function. It has a location in memory. Anyone can access that. – Bayleef Jun 26 '19 at 00:41
  • Nowhere does it say "they can not be accessed by the `this` pointer". It says that when static methods called, they have no `this` pointer. You can invoke them via `this`, but the `this` doesn't reach the static function. Static members can be accessed with `this` but they are not dependent upon `this`. (Some C++-inspired languages did not preserve the "you can access statics via `this`" feature in the new language.) – Raymond Chen Jun 26 '19 at 00:42
  • 1
    All your normal pointer rules apply with "*this" but the key is remembering that static functions have no "this" pointer and that static class members are shared by all instances. – Bayleef Jun 26 '19 at 00:42
  • @BaileyKocin Oh okay. I've used static members in the past, but I've never used them via the `this-pointer`. I've always used them through the scope resolution operator. `Foo::bar = somevalue` or `Foo::someStaticFunc();` – Francis Cugler Jun 26 '19 at 00:42
  • Yeah just wanted some clarity on it. I understand static storage, duration and linkage, but was a little confused about how they are or aren't related to a class's `this-pointer`. – Francis Cugler Jun 26 '19 at 00:44

1 Answers1

8

They aren't associated with the this pointer in your example. Rather, they happen to be accessible via the this pointer (for the same reason, f.bar = 10; would have been legal too).

This is covered explicitly in the C++ standard. See section "[class.static] Static members" (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4713.pdf), which states:

A static member s of class X may be referred to using the qualified-id expression X::s; it is not necessary to use the class member access syntax (8.5.1.5) to refer to a static member. A static member may be referred to using the class member access syntax, in which case the object expression is evaluated.

[ Example:

struct process {
  static void reschedule();
};
process& g();

void f() {
  process::reschedule(); // OK: no object necessary
  g().reschedule(); // g() is called
}

— end example ]

Barry
  • 286,269
  • 29
  • 621
  • 977
Nathan Rogers
  • 444
  • 2
  • 4