C + + - static member and member pointer

Article directory

Static member

Static member variable

Static member variable:

  Class class name{
  		static data type variable; / / declaration
  };
  Data type class name:: variable = initial value; / / definition and initialization
  • Ordinary member variables belong to objects, while static member variables do not belong to objects.
  • Static member variables are similar to global variables, stored in the global area, and can be understood as global variables restricted to use in classes.
  • Usage:
  Class name:: static member variable; / / recommended
  Object. Static member variable; / / equivalent to above

Code example

  • static.cpp
#include <iostream>
using namespace std;
class A{
public:
    //Common member variables are defined and initialized in constructors
    A(int data=0):m_data(data){}
    int m_data;
    static int s_data;//statement
};
//Static member variables are defined and initialized outside the class
int A::s_data = 100;//Definition
int main(void)
{
    A a(200);
    //Static member variable does not belong to object
    cout << sizeof(a) << endl;//4
    
    //Static member variables can be accessed through "class name::"
    cout << A::s_data << endl;//100
    //Ordinary member variables must be accessed through objects
    cout << a.m_data << endl;//200

    //Static member variables can also be accessed through objects
    cout << a.s_data << endl;//ok,100

    A a2(a);
    a2.m_data = 222;
    a2.s_data = 111;//A::s_data = 111

    cout << a.s_data << endl;//111
    cout << a.m_data << endl;//200

    return 0;

}
  • results of enforcement

Static member function

  Class class name{
  		static return type function name (parameter table) {...}	
  };
  • Static member functions do not have this pointers or constant properties.
  • Use:
	Class name:: static member function (argument table); / / recommended
	Object. Static member function (argument table); / / equivalent to the above
  • Note: only static members can be accessed in static member functions, not non-static members; both static members and non-static members can be accessed in non-static member functions.

Code example

  • static_func.cpp
#include <iostream>
using namespace std;
class A{
public:
    A(int data=0):m_data(data){}
    static void func1(void){
        cout << "Static member function" << endl;
        cout << s_data << endl;
        //cout << m_data << endl;
    }
    void func2(void){
        cout << "Nonstatic member function" << endl;
        cout << s_data << endl;
        cout << m_data << endl;
    }
    int m_data;
    static int s_data;//statement
};
int A::s_data = 100;//Definition
int main(void)
{
    A::func1();

    A a(200);
    a.func2();//A::func2(&a)
    return 0;

}
  • results of enforcement

Singleton mode

Concept: a class only allows unique objects and provides its access methods.

Creation idea:

  • Prevents the creation of objects outside of a class: a private constructor.
  • Class maintains unique objects internally: static member variables.
  • Provide the access method of singleton object: static member function.

Created by:

  • Starved Chinese style: whether used or not, the program is created when it is started.
  • Lazy: created in time, not destroyed.

Code example

  • hungry.cpp
//Single case model: starved Han style
#include <iostream>
using namespace std;
class Singleton{
public:
    //3) Using static member function to get singleton object
    static Singleton& getInstance(void){
        return s_instance;
    }
    void print(void)const{
        cout << m_data << endl;
    }
private:
    //1) Privatization constructor
    Singleton(int data=0):m_data(data){
        cout << "Singleton object created" << endl;
    }
    Singleton(const Singleton&);
private:
    int m_data;
    //2) Using static member variables to represent unique singleton objects
    static Singleton s_instance;
};
Singleton Singleton::s_instance(12345);
int main(void)
{
    cout << "main Function start execution" << endl;
    //Singleton s(100);
    //Singleton* ps = new Singleton(100);
    Singleton& s1=Singleton::getInstance();
    Singleton& s2=Singleton::getInstance();
    Singleton& s3=Singleton::getInstance();

    cout << "&s1=" << &s1 << ",&s2=" <<
        &s2 << ",&s3=" << &s3 << endl;
    s1.print();
    s2.print();
    s3.print();

    return 0;
}
  • results of enforcement

  • lary.cpp

//Singleton mode: lazy type
#include <iostream>
using namespace std;
class Singleton{
public:
    //3) Using static member function to get singleton object
    static Singleton& getInstance(void){
        if(s_instance == NULL)
            s_instance=new Singleton(12345);
        s_count++;
        return *s_instance;
    }
    //Single instance object is destroyed without use
    //The singleton object may have multiple users, which should be the last
    //A user uses release to delete
    void release(void){
        if(--s_count == 0){
            delete s_instance;
            s_instance = NULL;
        }
    }
    void print(void)const{
        cout << m_data << endl;
    }
private:
    //1) Privatization constructor
    Singleton(int data=0):m_data(data){
        cout << "Singleton object created" << endl;
    }
    Singleton(const Singleton&);
    ~Singleton(void){
        cout << "The singleton object is destroyed" << endl;
    }
private:
    int m_data;
    //2) Using static member variables to represent unique singleton objects
    static Singleton* s_instance;

    //Count: record the number of single object users
    static int s_count;
};
Singleton* Singleton::s_instance = NULL;
int Singleton::s_count = 0;

int main(void)
{
    cout << "main Function start execution" << endl;
    //Singleton s(100);
    //Singleton* ps = new Singleton(100);
    
    //s_count++ ==> 1
    Singleton& s1=Singleton::getInstance();
    //s_count++ ==> 2
    Singleton& s2=Singleton::getInstance();
    //s_count++ ==> 3
    Singleton& s3=Singleton::getInstance();

    cout << "&s1=" << &s1 << ",&s2=" <<
        &s2 << ",&s3=" << &s3 << endl;
    s1.print();
    s1.release();//--s_count,2
    
    s2.print();
    s3.print();

    s2.release();//--s_count,1
    s3.release();//--s_count,0 delete

    return 0;
}
  • results of enforcement

member pointer

Member variable pointer

  • Definition
Type class name:: * member pointer variable name = & Class Name:: member variable;
  • Use
Object. * member pointer variable name;
Object pointer - > * member pointer variable name;

Member function pointer

  • Definition
Return type (class name:: * member function pointer) (parameter table) = & Class Name:: member function name;
  • Use
	(object. * member function pointer) (argument table)
	(object - > * member function pointer) (argument table)
  • notes
  ". *" is called the direct member dereference operator
  "- > *" is called indirect member dereference operator

Code example

  • memptr.cpp
#include <iostream>
using namespace std;
class Student{
public:
    Student(const string& name)
        :m_name(name){}
    void who(void){
        cout << "My name is" << m_name << endl;
    }
    string m_name;
};
int main(void)
{
    //Member variable pointer
    string Student::*pname 
        = &Student::m_name;
    //Member function pointer
    void (Student::*pwho)(void) 
        = &Student::who;
    Student s("Jian Zhi Wang");
    cout << s.*pname << endl;
    (s.*pwho)();

    return 0;
}

  • results of enforcement
Published 95 original articles, won praise 26, visited 40000+
Private letter follow

Posted on Wed, 12 Feb 2020 18:41:21 -0800 by jalperin