Static member function of a class - C ++ Lesson 26

This article learn from Ditai Software College Tangzuo Lin teacher of C ++ courses


By demand: "you can always get the current number of objects" to elicit a static member function;
Option 1: The current number of objects to be private static read-only variables, you can get by member functions, but must be defined in order to obtain a new object to ,give up.

Scenario 2: The current number of objects is public static member variables, accessed via the class name, but very unsafe, can be freely modified, discarded.

Scenario 3: The current number of objects is set to static member variables private by public access to private static member variables static member function, and then access the public static member functions by class name or object name. feasible.


Experiment 1: Use static member function: public static member functions can be accessed directly through the class and object names

通过对象名直接访问 公有静态成员函数
通过类名直接访问 公有静态成员函数

Experiment 2: The public can retrieve the current object at any time the number of static member function

通过类名访问公有静态成员函数
通过对象名访问共有静态成员函数
通过指针访问共有静态成员函数

Here Insert Picture Description

Here Insert Picture Description

Here Insert Picture Description
Here Insert Picture Description

Experiment 1: Use static member function: public static member functions can be accessed directly through the class and object names

Access public static member function directly through the object name
public static member function accessed directly through the class name

#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;
}

int main()
{
    Demo::StaticFunc("main Begin...");
    
    Demo d;

	d.StaticSetI(d, 10);//通过对象名直接访问 静态成员函数
    
    Demo::StaticSetI(d, 10);//通过类名直接访问 静态成员函数
    
    printf("d.i = %d\n", d.getI());
    
    Demo::StaticFunc("main End...");
    
    return 0;
}


mhr@ubuntu:~/work/c++$ 
mhr@ubuntu:~/work/c++$ g++ 26-2.cpp
mhr@ubuntu:~/work/c++$ ./a.out 
StaticFunc: main Begin...
d.i = 10
StaticFunc: main End...
mhr@ubuntu:~/work/c++$ 

Here Insert Picture Description

Experiment 2: The number of the current object can be obtained through the static member function at any time
public static member functions accessible through the class name
to access a total static member function through the object name
by a total static member function pointer access

#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;
}
Published 207 original articles · won praise 100 · views 80000 +

Guess you like

Origin blog.csdn.net/LinuxArmbiggod/article/details/104108501