Examples of static and const usage in C++ classes

  • 2020-04-02 02:40:24
  • OfStack

Static and const are very important concepts in C++ programming. This paper illustrates the rules and usage of static and const in C++ class. For your reference. The details are as follows:

First, the code is used as an example. The sample code is as follows:


class A
{
public:
  A():m(10)         //Const members must be initialized in the constructor's initialization construct list
  {
    q = 40;
  }

  void fun1()const
  {
    m++;         //Error. Const members are constants whose values cannot be changed.
    n++;         //Correct. The static variable n belongs to the class, but every object's function can access and change it.
    q++;         //Error. The const member function cannot change the value of a data member.
  }
  static void fun2()
  {
    m++;         //Error. Const members are constants whose values cannot be changed.
    n++;         //Correct. A static member function can access and change the value of a static variable.
    q++;         //Error. A static member function cannot access and change the value of a non-static data member.
  }

  const int m;
  static int n;
  static const int p;
  int q;
};

int A::n = 5;        //The static member must be initialized outside the class. The static keyword is not used, but the class scope A:: is specified.
const int A::p = 30;     //The static const member is initialized outside the class as the static member (not in the constructor initialization list), and remember to add the keyword const

This is followed by a detailed explanation.

Static keyword

1. Static data member

A static data member is a member of a class, not of any concrete object, and does not occupy the object's memory space. It can be accessed either as A::n or through objects (which are not part of A concrete object, but are Shared by all objects).

Int A::n = 5; int A::n = 5; This way, remember to specify the type and the class to which it belongs, without adding the keyword static.

2. Static member function

A static member function can only access a static data member or a static member function, not a non-static data member or a non-static member function.

Two, const keyword

1. Const data member

You must initialize in the constructor initialization list. The reasons can be understood in two ways.

(1) class members cannot be initialized in the declaration, such as int c = 3 in the class body; No.

(2) cannot assign a value in a member function, because const cannot be changed.

2. Const member function

The const member function can access all data members, but it cannot change the value of any data member of the object, but it can change the value of the static member.

3. The const objects


const A a;

Const objects can only call const member functions and can only change static members.

Static const keyword

The first thing to remember is that a static const is both static and const.


static const int p;//Const static int p; It's the same thing.

Initialization is the same as a static member, initialized outside the class, but with const.


const int A::p = 30;

Hope that the article described in the C++ object-oriented programming has some help.


Related articles: