C + + const

Source: Internet
Author: User

C + + adds a const meaning to the function:
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, that is, 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.

A non-static member function that is followed by a const (added to a non-member function or a static member will result in a compilation error) indicates that the member function implicitly passes in the this pointer to a const pointer, which determines in the member function, Arbitrarily modifying the operation of a member of the class it is in is not allowed (because a const reference to the this pointer is implied); The only exception is for members that are mutable decorated. Const-Added member functions can be called by non-const objects and const objects, but non-const member functions can only be called by other than const objects. For example:
Class A
{
Private
int m_a;
Public
A (): m_a (0) {}
int Geta () const {return m_a;//same as return this->m_a;}
int Geta () {return m_a;}
int SetA (int a) const
{
M_a = A; This generates a compile error, if the previous member is defined as int m_a; mutable int m_a is changed; it can be compiled.
}
int SetA (int a)
{m_a = A;//with this->m_a = A;
}
};
A A1;
const A A2;
int t;
t = A1.geta ();
t = A1. Geta ();
t = A2.geta ();
t = A2. Geta (); A2 is a const object, and calling a non-const member function produces a compilation error.

Http://www.cnblogs.com/xudong-bupt/p/3509567.html

Summarize:

The ① class method is used later, int hhh const{... }, which means that the method cannot change the data member of the class

Ii

When const modifies pointer variables:

(1) There is only one const, if the const is on the left side, indicating that the pointer data is a constant, you cannot modify the data by dereferencing it; The pointer itself is a variable and can point to other memory units. (whichever is the same as the data type)

(2) There is only one const, if the const is on the right, indicating that the pointer itself is a constant and cannot point to another memory address; the data that the pointer refers to can be modified by a dereference.

(3) Two const,* each, indicating that the pointer and pointer data cannot be modified.

C + + const

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.