Language Selector

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
8.11 -- Static member variables

39 comments to 8.12 — Static member functions

  • tony

    Very good explanation and examples..

  • heya

    we don’t need to instantiate an object of the class to make use of it’s functionality!

    it’s should be its

    heh, this reminds me of editing wikipedia. Always correcting small errors no one notices but never adding anything useful.

  • San

    The examples are simply simple to understand the concept…Thanks a lot

  • anon

    Absolutely short and sweet !

  • peter

    Hi Alex,

    Did not understand the following….Can u explain?

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

    What do u mean by two IDgenerators? Is it two classes?
    Can some one explain the above paragraph in detail

    • 4lgor1thm

      Hi, Peter

      “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).”

      Static Classes can only have one instance derived from them (not an object, but a call to the static class itself.). Non-static classes can have an infinite number of instance derived from them (objects). Consider the example below; it’s an excerpt from a small Console app I wrote in Visual C++ Studio Express 2010:

      // StaticClasses.cpp : main project file.

      #include “stdafx.h”

      using namespace System;

      //Our pure static class can only contain static items …
      static class StaticClass
      static int s_nValue;

      static int getValue(){return s_nValue++;}

      class DynamicClass
      int m_nValue;

      DynamicClass(){m_nValue = 1;}
      int getValue(){return m_nValue++;}

      int StaticClass::s_nValue = 0;

      int main(array ^args)
      StaticClass cStaticClass;

      //cStaticClass::getValue(); //Throws an exception, cannot access static methods and variables of an instantiated object of a static class

      //in the tutorial the author said it can be instantiated once
      //This is the reason why it holds on to its value even out of class scope.
      //Here we have one copy, the static class
      Console::WriteLine(L”StaticClass getValue call: ” + StaticClass::getValue());
      Console::WriteLine(L”StaticClass getValue call: ” + StaticClass::getValue());

      //in order to have multiple copies of the class, we need to create a non-static class, and instantiate at least 2 objects
      DynamicClass cClass1;
      DynamicClass cClass2;

      //here 2 objects are being instantiated from a non-static class; here we have 2 copies, the same way we could use 2 different IDGenerators.
      Console::WriteLine(L”Instantiated Object 1: ” + cClass1.getValue());
      Console::WriteLine(L”Instantiated Object 2: ” + cClass2.getValue());

      Console::WriteLine(L”Press any key to continue …”);
      return 0;


      “… if you needed two independent IDGenerators, this would not be possible.”

      You can only derive one instance from a static class, and it is not an object.

      I Hope this helps …


      • peter

        Thanks 4lgor1thm…:)

      • uma

        //cStaticClass::getValue(); //Throws an exception, cannot access static methods and variables of an instantiated object of a static class

        this error i am not getting
        cStaticClass.getvalue();---> i have written iike this i am not getting any error
        cStaticClass is a object right so we can access member function or data of the claa by dot operator right.

        y can u explain me in detail why i am not getting error

      • Seems STATIC classes are not supported under C++ … ??

        When I tried to compile the above code I get error saying
        “error: a storage class can only be specified for objects and functions”

  • SUN

    Hello Alex,

    Can you please explain me what is the use of making a constructor static and when we need to do that ?


      hi, SUN, I think static constructor is used for :
      1.initiating some static member variables of a class;
      2.if you don’t write the static constructor, and your class has some static member variables which already given the initial values, the compiler will automated generate a static constructor for you.
      3. I think in database singleton design mode, it is useful. Because we don’t want to generate too much accessing thread for the database, it is waste the resource.


  • The whole of this example, with all of the static variables and methods, produces dangerous code. Threading becomes difficult (you have to build your own locks) and anybody can get to the variables from anywhere without the richness of the various c++ tools like boost, etc. This is completely done without problems, allowing the richness of the various other libraries that are aimed at OS management with a Singleton Pattern. I like the example (a href=”” title=”here.”>

  • ankit shukla

    friend function in c++ is that in which the two classes directly communicate each other.but it breaks the priciple of data hiding, is it

  • deajosha

    thanks a lot for sharing.

  • Debdipta

    What is difference in ‘Static’ in C and C++?

  • 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

    using namespace std;
    class static_try
    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

    hope you have got it.Thanks

  • 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

  • stheurkar

    class IDGenerator
    static int s_nNextID;

    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.

  • 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 :)

  • I’m confused. Just before you describe the use of public static member functions to access private static member variables, you mention that “we can’t access Something::s_nValue directly from main(), because it is private.” In the code snippets, however, on line 11 you initialize s_nValue to 1 directly. Why does this work? Shouldn’t it fail since you are attempting to access a private member variable directly, even though it is static? Or does the limitation only apply inside a function, such as main()?

  • Lorence

    <b>Hello alex,</b>

    <p> Can you explain this to me? </p>

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

  • Henri de Feraudy

    Something seriously missing in your article:
    how to declare a static function outside of the class declaration (in the source file).
    Your example has a static method defined inside the class Something{};

  • Piyoosh

    why static member function able to access private constructor

    Please explane
    class abc
    static abc*instance;
    staic abc* getinstance()
         instance = new abc();
    return instance

    abc *abc::instance =NULL;

  • puppi

    hello everybody, i have a question.
    in below code, why can i not be able to delete which previously i have been created instance? what’s wrong with my code?

    thanks a lot.

    • Alex, Puppi’s problem is mysterious to me too. Can you answer us..?

      • Alex

        You can’t explicitly delete variable sn because it hasn’t been dynamically allocated.

        When sn goes out of scope, the destructor will naturally be called, which in this case will delete static variable inst, which is what you want anyway.

        • Avneet

          I removed that line + "pause"( may be it’s a system call), put an output statement just after dynamically allocating memory like this:

          and a strange thing happens. This line get printed again and again like an infinite loop. I moved that line from there to destructor before calling
          getInstance (), the same thing happens,  it goes infinite. Then I placed that line of code below the call to getInstance () in destructor. Nothing printed and program crashes. Why????

          • Alex

            When sn goes out of scope, the destructor is called. The destructor calls Sample::getInstance(), which allocates an instance of Sample (which it really shouldn’t be doing in a destructor, but lets put that aside) and then immediately deletes it (without setting inst back to null, so now inst is a dangling pointer). However, deleting it causes the destructor to be executed again. Since inst isn’t null, it returns the dangling inst pointer, and tries to delete it again. This has undefined effects. In the best case, deleting it again will cause the destructor to be called again, re-deleting inst, causing the destructor to be called again, re-deleting inst, etc… An infinite loop.

            In the worst case, it will crash.

            In short, this program is extremely ill-formed. It’s weird for a class to manage a static instance of itself. It leaves a dangling pointer after deleting the return value of getInstance(). And because inst is inside getInstance(), there’s no way to set inst to null once it is deleted.

  • redhotchilialex

    s_nNextID is private but we can access it directly outside the class declaration. I didn’t understand this…can anyone explain me, please?

    • Alex

      Static member variables are a little weird. There’s only one instance shared across all instance of the class. For this reason, although the static member variable is declared inside the class, it is defined _outside_ of the class. For this reason, the initialization happens outside of the class too.

      Note that because s_nNextID is private, you can’t use it outside of the class -- this definition is purely for initialization purposes.

      • Gopal

        I think this is the only thing i have to memorize coz it is out of logic circle.

        "There’s only one instance shared across all instance of the class." -- This not seems logical reason.  

        As per my understanding if it is private, it must not change valve out side of class whether its static or not, but here it is happening. Its strange.

        Even this below defination also seems strange coz we have to again apply type int to the definition even though we declared type at the time of definition inside of class.

        int IDGenerator::s_nNextID = 1;

        • Alex

          The access specifiers only restricts whether a variable can be accessed directly from outside the class or not. A static member is treated just like a normal member in this regard. The fact that there is only one instance of a static variable shared across all member of the class is irrelevant to how it is accessed.

          The static member variable is _declared_ inside the class, but it needs to be initialized outside of the class, at the point where it is _defined_. I agree that the way it is initialized is a little funny looking.

  • sameer

    what is use for "Static member functions",
    what is different between "Static member functions" and "globa functions".


    • Alex

      Static member functions are primarily useful for accessing static member variables.

      There isn’t much difference between a static member function and a global function, outside of the fact that the static member function is associated with your class whereas a global function isn’t. This means static member functions can access the private static data members of the class, whereas global functions can’t.

Leave a Comment




17 − 4 =

Put C++ code inside [code][/code] tags to use the syntax highlighter