The difference between const and non-const in C + +

Source: Internet
Author: User
Tags gety

The difference between "constant" and "read-only variable". 

Constants are definitely read-only , such as 5, "ABC", and so on, which is definitely read-only, because constants are read-only areas that are placed in memory by the compiler, and certainly not able to modify it.

a "read-only variable" is a place in memory to store its value, except that this value is not allowed to be modified by the compiler. The C keyword const is a modifier (Qualifier) that defines a variable that is not allowed to be changed.

in the ANSI C language, both enum types and # define macros are used to define constants.

A const is used in a type declaration to decorate a constant, as in the following two ways.

1), const in front
const int Nvalue;//nvalue is const
const char *pcontent;//*pcontent is const, pcontent variable
Const (char *) pcontent;//pcontent is const,*pcontent variable
char* const pcontent;//pcontent is const,*pcontent variable
const char* const pcontent;//pcontent and *pcontent are const

2), const in the back, with the above declaration peering
int const nvalue;//Nvalue is const
Char Const * pcontent;//*pcontent is const, pcontent variable
(char *) const pcontent;//pcontent is const,*pcontent variable
char* Const pcontent;//pcontent is const,*pcontent variable
Char const* const pcontent;//pcontent and *pcontent are both const

3), const and pointers
int me; const int * P1=&ME;//P1 variable, *P1 immutable, cannot be modified with *P1 at this time, but P1 can turn
The int * Const P2=&ME;//P2 immutable, *P2 variable, allowing *P2 to modify its value at this time, but P2 cannot turn.
The const int *const p3=&me;//p3 Immutable, *P3 also immutable, at which point the value cannot be modified with *P3, nor can it be turned

Const USE:
1. Two cases for pointers: const is a left-associative type modifier.

int const *a;//a variable, *a immutable
int *const A;//a immutable, *a variable

2. Pass-through value parameters of the qualifying function:

void function (const int Var);//The passed argument cannot be changed within the function.
3. The qualifying function returns a value type.

const int function ();//const Meaningless at this time
The const myclassname function ();//functions return the custom type myClassName.

4 Qualifying function type.

void function () const;//constant member function, const member function cannot change the object's member function.
Example: int point::gety ()
{
return yval;
}

When this function is called, the point object is not changed, and the following function changes the point object:

void point:: setpt (int x, int y)
{
xval=x;
yval=y;
}
to make the meaning of the member function clearer, we can add a const description to the function prototype that does not change the member function of the object:
class Point
{
Public :
int GetX () const;
int GetY () const;
void setpt (int, int);
void offsetpt (int, int);
Private:
int Xval,
Yval;
};

the const member function should add a const qualification to both the function prototype description and the function definition:

int point::gety () const
{
return yval; }

class Set
{
Public :
Set (void)
{card = 0;}
bool Member (const int) const;
void Addelem (const int);   ...   };
bool Set::member (const int elem) Const
{//...}

a const member function cannot be called by a constant member object because it may attempt to modify a constant's data member:

Const SET S;
S.addelem (10);//illegal: Addelem is not a constant member function
S.member (10);//Correct

******* but constructors and destructors have exceptions to this rule, which are never defined as constant members, but can be called by constant objects (called automatically). They can also assign values to the data members of a constant, unless the data member itself is a constant.

when the const code snippet does not contain parentheses, the line is uniform along the * number, and if the const is on the left side of *, the const is used to decorate the variable pointed to by the pointer, that is, the pointer is constant; If Const is on the right side of *, const is the modifier pointer itself. That is, the pointer itself is a constant.   According to this rule you can see the actual meaning of the above statement, I believe it will be at a glance.
In addition, it is important to note that for const (char *), because char * is a whole, equivalent to a type (such as char), the qualified pointer is const.

a simple method of judging: The pointer operator *, from right to left, such as Char const * Pcontent, can be understood as Char const (* pcontent), that is, the * pcontent is const, and pcontent is mutable.

in the member functions of the class we define, there are often some member functions that do not change the data members of the class, that is, the functions are "read Only" functions, and some functions modify the values of the class data members. If a function that does not change the data member is labeled with the Const keyword, it is obvious that the readability of the program can be improved. In fact, it can improve the reliability of the program, has been defined as a const member function, once the attempt to modify the value of the data member, the compiler is handled by error.

Const member functions and const objects

In fact, the const member function has another function, which is the constant object correlation. For built-in data types, we can define their constants, as well as user-defined classes, which can define their constant objects.

For example, a method that defines an integral type constant is:

const int I=1;

Similarly, you can define a const object, assuming that there is a class ClassA, the method that defines the constant object of the class is:

Const ClassA A (2);

here, A is a const object of class ClassA, and "2" is passed to its constructor parameter. The data members of a const object cannot be changed during the lifetime of the object.   But how do you ensure that the data members of this class are not changed? To ensure that the data members of a const object are not changed, the const object can only call the const member function in C + +. If a member function does not actually make any kind of modification to the data member, it is not qualified by the Const keyword and cannot be called by a constant object. Here's an example to illustrate the problem:

class C
{
int X;
Public :
int GetX () {return X; }
void SetX (int X) {this->x = X;   }   };
void Main () {const C CONSTC;   Cout<<constc.getx (); }

if we compile the program code above, the compiler will get an error: CONSTC is a constant object, it can only call the const member function. Although the Getx () function does not actually change the data member X, it cannot be called by the CONSTC object because there is no const keyword qualification. If we put the above code in:

int GetX () is rewritten as: int GetX () const and recompile again, there is no problem.

use of the const member function

the const member function indicates that the member function can read only class data members, and cannot modify class member data. When defining a const member function, place the const keyword between the function's parameter table and the function body. One might ask: why not put a const before a function declaration? Because doing so means that the return value of the function is constant and the meaning is completely different. Here is an example of defining a const member function:

class X
{int i;
Public :
int f () const;   };

The keyword const must be repeated in the function implementation in the same way, or the compiler will think of it as a different function:

int x::f () const
{return i; }

if f () attempts to change I in any way or call another non-const member function, the compiler will give an error message. Any function that does not modify member data should be declared as a const function, which helps to improve the readability and reliability of the program.

--Reproduced in Baidu know

The difference between const and non-const in C + +

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.