C++ regular objects and regular object members

  • 2020-04-02 01:49:50
  • OfStack

Often the object

A constant object must be specified as a constant object when an object is defined.

A data member in a constant object is a constant variable and must have an initial value, such as


Time const t1(12,34,36); //Define t1 as a constant object

This way, in all cases, the values of all data members in object t1 cannot be modified. Objects that wish to ensure that data members are not changed can be declared as constant objects.

The general form of a constant object is defined as

The name of the class       const       Object name (argument list);

You could also write the const on the far left

const       The name of the class       Object name (argument list);

The equivalent

If an object is declared a constant object, the non-const-type member functions of that object cannot be called (except for implicit constructors and destructors that are called automatically by the system).

Otherwise you will report an error
< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201310/20130905131437515.png ">

This is done so that non-const member functions can modify the values of data members in regular objects, because const member functions cannot modify the values of data members in objects (more on this later).

So, how do you reference data members in constant variables? Quite simply, we just need to declare the member function as a const-type member function (constant member function).


void Print() const;

A constant member function can access a data member in a regular object, but it is still not allowed to modify the value of a data member in a regular object.

Sometimes programming requires that the value of a data member in a constant object member be changed (for example, if there is a variable count in the class for counting, the value should not change),

The data member is declared mutable, such as


mutable int count;//Defines a data member that can be changed in a regular object

Declare count as a mutable data member so that its value can be modified with the member function declared as const.

= = = = = = = = = = = = = = = = = = = = = = = = = a simple example program 1.1 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =


#include<iostream>
using namespace std;
class Student
{
<span style="white-space:pre"> </span>public:
<span style="white-space:pre">  </span>Student(int n,float s):num(n),score(s){}
<span style="white-space:pre">  </span>void change(int n,float s) const{num=n;score=s;}
<span style="white-space:pre">  </span>void display() const{cout<<num<<"t"<<score<<endl;}
<span style="white-space:pre"> </span>private:
<span style="white-space:pre">  </span>mutable int num;
<span style="white-space:pre">  </span>mutable float score;
} ;
int main()
{
<span style="white-space:pre"> </span>Student const stud(101,78.5);
<span style="white-space:pre"> </span>stud.display();
<span style="white-space:pre"> </span>stud.change(101,80.5);
<span style="white-space:pre"> </span>stud.display();
<span style="white-space:pre"> </span>return 0;
};

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201310/20130905133409390.png ">

We defined the object often stud and then invoke the two often function display () and change (), but in the change function is to change often object in num and score of value, so we defined num and score as a mutable.
Constant object member

1. Permanent data member
Its function and usage are similar to that of ordinary variables, and the keyword const is used to declare regular data members. The value of a constant data member cannot be changed.
Constant data members can only be initialized through the constructor's parameter initialization table.

Declare a constant data member in the class body


const int num;//Declare hour to be a constant data member

Define a constructor outside the class

Student::Student(int n,float s):num(n),score(s){} //Initialize num and score by initializing the table with parameters

After a data member is declared as a constant data member in the class body, the value of the data member in all objects of the class cannot be changed, but the value of the variable member in different objects can be different (specified separately at initialization).

2. Constant member function

If a member function is declared as a constant member function, the data members in this class can only be referenced, not modified.
Note: a constant object can only reference a constant member function

The definition form of the constant member function:


void Print() const;//Notice that the const is placed after the function name and parentheses

Const is a part of the function type. The const keyword should be used to declare the function and define the function.

Constant - member functions can const data members, can also refer to non-const data members, but can not modify them;

Functions that are not always members can call const data members, but cannot modify them, or can call non-const data members, and can modify them.

The specific situation is shown in figure 1:

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201310/20130905234340109.png ">

There are three other points to note:
1. Do not mistakenly believe that the member function in the constant object is a constant member function, the constant object only guarantees that the value of all its data members will not be modified.

2. If a member function in a regular object is not const declared, the compilation system will treat it as the least const member function.

3. Also note that a regular member function cannot call another non-cosnt member function.


Related articles: