Static

Hello,

I don't really understand what static member is.
Can somebody explain to me what is static member ??

Thanks..
You do know global variables? Static class member variable is very similar, with the exception that the class dictates who can access that variable.

Static member function is similar to standalone function, but again with access controlled by the class.
If a class has a static member variable, then every instance of the class uses the SAME static member variable.

Imagine a class:

1
2
3
4
5
class person
{
  public:
  string name;
};


You could make an object of type person, and set their name variable to something.
You could make another object of type person, and set their name variable to something different.

1
2
3
4
5
person firstPerson;
firstPerson.name = "Mike";

person secondPerson;
secondPerson.name = "Sally";


Two different objects, each with their own independent name member variable.


If we change the class like this:

1
2
3
4
5
class person
{
  public:
  static  string name;
};


then EVERY person object we create will share one name member variable. They will all use the same one. If you change the name of one person object, you change the name of ALL of them, because they all share the same name member variable, because it is a static member variable.
@Repeater
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
using namespace std;

class test
{
public:
    static int x;
};

int main()
{
    test a;
    test b;
    a.x = 1;
    b.x = 2;
    cout << a.x << '\n' << b.x;
}


So, why does this code error?
I expect a.x and b.x have the same value because when I I thought that when I change b.x become 2, a.x also change become 2 also

Thanks..
test::x isn't initialised defined (memory set aside).

Put
int test::x = 0; or just int test::x;
on line 9 (between the declaration of the class and int main()).

EDIT: better explanation from @Repeater below.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
using namespace std;

class test
{
public:
    static int x;
};

int main()
{
    test a;
    test b;
    a.x = 1;
    b.x = 2;
    cout << a.x << '\n' << b.x;
}


Because every object of class test will share a single, common static int x; value, when you create an instance of the test class, it does NOT create an int x for itself. Makes sense, right?

They're all going to share one common int x , so they can't all go around creating an int x for themselves.

But there still has to be an int x for them all to use created somewhere. A single int x created, for these classes to use and share.

You have to create this yourself. Usually done in a cpp file somewhere. For example, in the way that lastchance suggests above.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>

class test
{
public:
    static int x;
};

int test::x = 0;

int main()
{
    std::cout << test::x << '\n';
    test::x = 42;
    // the test::x exists whether any objects are created or not
    test a;
    test b;
    std::cout << a.x << '\n' << b.x;
}
Topic archived. No new replies allowed.