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...