The static member function of class C + +

Keywords: C++ Programming

Unfinished requirements

  • Count the number of objects of a class during program running
  • Ensure the security of the program (global variables cannot be used)
  • Get the number of current objects at any time (Failure)

Change experiment: an attempt at a solution

#include <stdio.h>

class Test
{
public:
    static int cCount;
public: 
    Test()
    {
        cCount ++;
    }
    ~Test()
    {
        -- cCount;
    }
    int getCount()
    {
        return cCount;
    }
};

int Test::cCount = 0; 

int main()
{
    printf("count = %d\n", Test::cCount);
    
    Test::cCount = 1000;
    
    printf("count = %d\n", Test::cCount);

    return 0;
}
Output:
count = 0
count = 1000

Question:
The security of static variables is not guaranteed

problem analysis

  • What do we need?

    • Object independent access to static member variables
    • The security of static member variables must be guaranteed
    • Get the value of static member variable conveniently and quickly

Static member function

  • Static member functions can be defined in C + +

    • Static member function is a special member function in a class
    • Static member functions belong to the entire class
    • You can directly access the public static member function through the class name
    • Public static member functions can be accessed through object names
    • this pointer is not implied
    • Cannot access ordinary member variable (function), can access static member variable (function)

  • Definition of static member function

    • Modifying member functions directly through the static keyword

class Test
{
public:
    static void Func1() {};
    static int Func2();
};

int Test::Func2()
{
    return 0;
}

Programming experiment: static member function example

#include <stdio.h>

class Demo
{
private:
    int i;
public:
    int getI();
    static void StaticFunc(const char* s);
    static void StaticSetI(Demo& d, int v);
};

int Demo::getI()
{
    return i;
}

void Demo::StaticFunc(const char* s)
{
    printf("StaticFunc: %s\n", s);
}

void Demo::StaticSetI(Demo& d, int v)
{
    d.i = v;                            // Accessing common member variables through objects 
}

int main()
{
    Demo::StaticFunc("main Begin...");  // Accessing static member functions by class name
    
    Demo d;
    
    d.StaticSetI(d, 10);                // Accessing static member functions by object name
    
    printf("d.i = %d\n", d.getI());
    
    Demo::StaticFunc("main End...");

    return 0;
}

Static member function VS ordinary member function

Static member function Common member function
All object sharing Yes Yes
Implied this pointer No Yes
Accessing common member variables (functions) No Yes
Accessing static member variables (functions) Yes Yes
Called directly by class name Yes No
Called directly by object name Yes Yes

Programming experiment: the final solution

#include <stdio.h>

class Test
{
private:
    static int cCount;
public: 
    Test()
    {
        cCount ++;
    }
    ~Test()
    {
        -- cCount;
    }
    static int GetCount()
    {
        return cCount;
    }
};

int Test::cCount = 0; 

int main()
{
    printf("count = %d\n", Test::GetCount());
    
    Test t1;
    Test t2;
    
    printf("count = %d\n", t1.GetCount());
    printf("count = %d\n", t2.GetCount());
    
    Test* pt = new Test();
    
    printf("count = %d\n", pt->GetCount());
    
    delete pt;
    
    printf("count = %d\n", Test::GetCount());

    return 0;
}
Output:
count = 0
count = 2
count = 2
count = 3
count = 2

Summary

  • Static member function is a special member function in a class
  • Static member function has no hidden this parameter
  • Static member functions can be accessed directly through the class name
  • Static member functions can only access static member variables (functions)

The above contents refer to the series courses of Ditai Software Institute, please protect the original!

Posted by Tindo on Sat, 30 Nov 2019 16:04:58 -0800