Search

8.12 — Static member functions

In the previous lesson on static member variables, you learned that classes can have member variables that are shared across all objects of that class type. However, what if our static member variables are private? Consider the following example:

In this case, we can’t access Something::s_nValue directly from main(), because it is private. Normally we access private members through public member functions. While we could create a normal public member function to access s_nValue, we’d then need to instantiate an object of the class type to use the function! We can do better. In this case, the answer to the problem is that we can also make member functions static.

Like static member variables, static member functions are not attached to any particular object. Here is the above example with a static member function accessor:

Because static member functions are not attached to a particular object, they can be called directly by using the class name and the scope operator. Like static member variables, they can also be called through objects of the class type, though this is not recommended.

Static member functions have two interesting quirks worth noting. First, because static member functions are not attached to an object, they have no this pointer! This makes sense when you think about it -- the this pointer always points to the object that the member function is working on. Static member functions do not work on an object, so the this pointer is not needed.

Second, static member functions can only access static member variables. They can not access non-static member variables. This is because non-static member variables must belong to a class object, and static member functions have no class object to work with!

Here’s another example using static member variables and functions:

This program prints:

The next ID is: 1
The next ID is: 2
The next ID is: 3
The next ID is: 4
The next ID is: 5

Note that because all the data and functions in this class are static, we don’t need to instantiate an object of the class to make use of it’s functionality! This class utilizes a static member variable to hold the value of the next ID to be assigned, and provides a static member function to return that ID and increment it.

Be careful when writing classes with all static members like this. Although such “pure static classes” can be useful, they also come with some potential downsides. First, because all of the members belong to the class, and the class is accessible from anywhere in the program, it’s essentially the equivalent of declaring a global variable of the class type. In the section on global variables, you learned that global variables are dangerous because one piece of code can change the value of the global variable and end up breaking another piece of seemingly unrelated code. The same holds true for pure static classes. Second, because all static members are instantiated only once, there is no way to have multiple copies of a pure static class (without cloning the class and renaming it). For example, if you needed two independent IDGenerators, this would not be possible.

8.13 -- Friend functions and classes
Index
8.11 -- Static member variables

23 comments to 8.12 — Static member functions

  • calicratis19

    Hi I love your tutorial very much. Its like having a discussion with friend. It helped me a lot :) Thanks for that. Today I tried to read about static keyword in here. It was really confusing to me when it comes to the linkage. I get really confused about the linkage of static and inline functions. Can you please write a tutorial about that too? I think its a really important topic missed by learncpp. It will be really helpful also. Thanks again for your tremendous effort with this blog. A lot of us are very grateful :)

  • stheurkar

    class IDGenerator
    {
    private:
    static int s_nNextID;

    public:
    static int GetNextID() { return s_nNextID++; }
    };

    // We'll start generating IDs at 1
    int IDGenerator::s_nNextID = 1;

    How is it possible to access s_NextID defined under private access specifier in the line above.

  • mohsenkazemi

    hi everyone,
    i have a question here, i want to define a member function a static function because i want to create a thread on it, but as you said a static member function only have access to static variable, when i was working with MFC flexgrix forms i cant't define those variable(variables how handle the grid forms) as a static variable, please guide me to resolve that,
    best regards

  • tunvir rahman tusher

    Static is an alternative name of global variable.When you define a variable static its only one copy will be created and all object must share the same copy of the variable.
    try this one

    #include
    using namespace std;
    class static_try
    {
    public:
    static_try(int c){b=c;}
    static int a;////static variable(one copy will be created!)
    int b;

    };
    int static_try::a=1;/////must be initialized outside class
    int main()
    {
    static_try s(2);
    // s.out();
    cout<<s.a<<" "<<s.b<<endl;////s share a
    static_try s1(3);
    cout<<s1.a<<" "<<s1.b;///s1 also share a
    getchar();

    }
    hope you have got it.Thanks

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">