C ++ const qualifier Summary

Source: Internet
Author: User
Tags gety

 

1. Define a const object

Const converts an object into a constant:

Const int bufsize = 512; Define the bufsize as a constant and initialize it to 512.

Since constants cannot be modified after definition, they must be initialized during definition.

 

2 const reference

 

Const int ival = 1024;

Const Int & refval = ival;

 

The const reference can be bound to a different but related type object or to the right value.

Int I = 42; double dval = 3.14;

Const Int & R = 42;

Const Int & r2 = R + I;

Const Int & rI = dval;

 

Non-const references can only be bound to objects of the same type as the reference.

Cause:

Double dval = 3.14;

Const Int & rI = dval;

The compiler converts the code into the following format:

Int temp = dval;

Const Int & rI = temp;

If Ri is not a const reference, the RI value can be changed. If Ri is changed, but temp does not change dval, it obviously does not conform to the attribute that reference is the alias of the object.

 

 

3 pointer to the const object

Const double * cptr; cptr is a pointer to a double-type const object. Const limits the object type pointed to by cptr pointer, not cptr itself. That is to say, cptr itself is not a const pointer. If necessary, you can assign a value to cptr to point it to another const object. However, cptr cannot be used to modify the value of the object.

 

* Cptr = 42; // incorrect. You cannot use cptr to modify the const object to which it points.

 

You cannot assign the const object address to a common non-const Object Pointer (the reason is obvious)

 

 

4. Const pointer

 

Const pointer. The value cannot be modified. (A pointer is a special variable, and the value stored in it is interpreted as an address in the memory, that is, the address stored in the pointer will not change.) like any const, the const pointer must also be initialized during definition.

Int errnumb = 0;

Int * const curerr = & errnumb; curerr is the const pointer to an int object.

The const Pointer Points to an object that is not const, so it can be changed.

* Curerr = 0; // OK

 

 

5. Const pointer to the const object

Const double Pi = 3.14159;

Const double * const pi_ptr = & PI;

It can be seen from that the value of the object pointed to by pi_ptr cannot be modified, or the pointer cannot be modified (that is, the address value stored in pi_ptr). It must be initialized during definition.

 

 

6. const_iterator

Each container type defines a type named const_iterator. This type can only be used to read elements in the container, but cannot change its value. It is similar to the 3rd point mentioned above.

For (vector <string >:: const_iterator iter = text. Begin (); iter! = Text. End (); ++ ITER)

* Iter = "; // incorrect.

 

7. Const iterator

(Similar to the 4th point mentioned above)

Vector <int> Nums (10 );

Const vector <int>:; iterator CIT = nums. Begin ();

* CIT = 1; // OK

++ Cit; // incorrect.

 

8. Dynamic Allocation and recovery of const objects

 

Const int * PCI = new const int (1024 );

For a const Dynamic Object of the class type, if the class provides the default constructor, this object can be implicitly initialized; class objects with built-in type objects or without Default constructors must be explicitly initialized.

Delete the const object Delete PCI;

 

 

9 const object const

Const cannot be declared as Const. When creating a const object of the class type, run a common const function to initialize the const object. A constructor initializes an object. Whether the object is const or not, a constructor is used to initialize the object.

 

10 const member functions

Some member functions change objects, while some member functions do not change objects.
For 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 member functions clearer, we can add const to the function prototype of the member functions without changing the object:
 

 

Class Point
{
Public:
Int getx () const;
Int Gety () const;
Void setpt (INT, INT );
Void offsetpt (INT, INT );
PRIVATE:
Int xval, yval;
};
 

Const member functions should be added with the const limitation in both the function prototype description and 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 non-constant member function cannot be called by a constant member object because it may attempt to modify the data member of a constant:
Const set S;
S. addelem (10); // invalid: addelem is not a constant member function.
S. Member (10); // correct
Except for this rule, constructor and destructor are never defined as constant members, but can be called by constant objects (automatically called ). They can also assign values to the data members of constants unless the data members are constants themselves.
Why do we need a const member function?
In the member functions defined by the class, some member functions do not change the data members of the class. That is to say, these functions are read-only functions, some functions need to modify the values of class data members. If you add the const keyword to all functions that do not change data members for identification, it can obviously improve the readability of the program. In fact, it can also improve the program's reliability. It has been defined as a const member function. Once it attempts to modify the value of the data member, the compiler will handle it by mistake.
Const member functions and const objects
In fact, the const member function has another function, that is, constant object correlation. For built-in data types, we can define their constants, and user-defined classes can also define their constant objects. For example, the method for defining an integer constant is:
Const int I = 1;
You can also define a constant object. Assume that there is a class classa, and the method for defining the constant object of this class is:
Const classa A (2 );
Here, a is a const object of the classa class, And the constructor parameter passed to it by "2. The data member of the const object cannot be changed during the life cycle of the object. However, how can we ensure that the data members of this class are not changed?
To ensure that the data member of the const object is not changed, in C ++, the const object can only call the const member function. If a member function does not actually modify the data member in any form, but it is not limited by the const keyword, it cannot be called by a constant object. Here is an example to illustrate this 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 above program code, the compiler will encounter an error message: constc is a constant object, which 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 restriction. If we use the above bold code:
Int getx ()
Rewrite:
Int getx () const
Then re-compile the program.
Use of const member functions
The const member function indicates that the member function can only read class data members, but cannot modify class member data. When defining a const member function, place the const keyword between the parameter table of the function and the function body. Some may ask: why not place const before function declaration? This means that the return value of the function is a constant and the meaning is completely different. The following is an instance that defines the const member function:
Class X
{
Int I;
Public:
Int F () const;
};
The const keyword must be repeated in function implementation in the same way; otherwise, the compiler will regard it as a different function:
Int X: F () const
{
Return I;
}
If F () tries 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 improve the readability and reliability of the program.

Object. member functions

Object member functions
1. Const const pair
2. Const non-const Error
3. Non-const pair
4. Not-const non-const pair

Member functions call member functions

Member function
5. Const const pair
6. Const non-const Error
7. Non-const pair
8. Non-const non-const pair

 

 

In short, the volume defined as const cannot be changed, and any attempt to change will cause errors.

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.