We know that if you do not want to change the value of a variable, you can use const to modify it and change it to a constant. Both the basic type and pointer type can be modified using Const. Arrays can also be modified using Const.
Then you naturally want to ask us to instantiate the class into an object,If you do not want to modify the member variables in an object, how can the object be adjusted like a constant?
First, you will naturally think of modifying all the member variables in the class with Const. in fact, this is indeed a way to pull, but if we just want an instance object of a class to make it like a constant. in this way, you can use the const integer to actually become a constant. All classes can only be processed as constants. this is obviously not feasible.
In fact, it is very simple. We only need to add a const when instantiating the class, but the class is not a simple type. therefore, some special operations are involved. let's take a simple example.
Class myclass
{
Public:
Int age;
String name;
Myclass (INT num, string Str)
{
Age = num;
Name = STR;
}
Void test () {print ();}
Void print ()Const
{
STD: cout <name <Endl;
}
~ Myclass (void );
};
Const myclass my (110, "Arwen"); // use const for class instantiation.
My. Age = 911; // This will cause an error. Since const is modified, it will certainly become like a constant, and the value of the member variable cannot be changed.
My. Print (); // run the command correctly.
My. Test (); // Error
Why does an error occur when I call my. Test? This is special. after a const object is modified, this object can only call the const modified function (the const must be placed after the function change ). A common function cannot be called.
This is actually very simple, for consistency considerations. since the member variables cannot be modified directly. any potential operations that may modify member variables are prohibited. generally, a function may modify the value of a member variable. therefore, the call is simply prohibited.
What does it mean to add a const after a function? It indicates that the value of the member variable cannot be modified in this function. For example:
Void print () const {age = 456;} // This will cause an error. Of course, if you modify other values that are not member variables in the function, no problem.
If other functions are called in the const-modified function, the called function can only be modified later by const. Of course, there is no limit for the function to call the const-modified function.
So after the object is modified with const
1. member variables cannot be directly modified like my. Age = 911
2. Objects can only call functions modified by const.
3. The member variable cannot be modified in the function modified by const, and it can only call other functions modified by const.
These three points ensure that the member functions will not be modified under any circumstances and remain unchanged after being assigned a value using the constructor.
The const modifier mentioned above can also be used independently. It may not be used together with the const modifier object. The same is true if it is used independently. member variables cannot be modified in the function.
In C ++, what about C?
First, the member variables in C # are generally called fields. unlike C ++, C # can directly assign values to fields. therefore, it is okay to modify fields in the class with const and then assign values. however, we usually want to assign values using constructors. what about it? Is it okay to use a const to modify it like C ++. no. because when the reference type is modified with const, only null can be assigned, which means that the object is useless. of course, C # Naturally there are other operations that can achieve similar results. that is, the permission modifier is added through the attribute.
Class myclass
{
Private int age;
Private string name;
Public int age {
Get {return age ;}
}
Public int name {
Get {return name ;}
}
Myclass (INT num, string Str)
{
Age = num;
Name = STR;
}
}
If you do not want to modify fields. set all fields to private. then, an interface is provided with properties. only the read permission is allowed and cannot be modified. only the constructor can assign values once. then you may ask if other member functions will be modified?
This requires the following conventions: Do not use fields directly for other functions in the class, or use attributes such as age and name. of course, this is only a recommended practice. If you really want to use fields directly, modify them. then there is no way.
C # uses attributes. We recommend that you use attributes directly in member functions. Do not use fields directly.
Another way is to use readonly to modify the field so that the field can only be read and cannot be modified.