The const function in C + +

Source: Internet
Author: User

In C + +, only member functions declared as const can be called by a Const class object.

To declare a class member function of a const type, simply add the keyword const after the member function argument list, for example,

Class Screen {public  :     Char get () const;  };  

When you define a const member function outside the class body, you must also add the const keyword, such as

Char screen::get () const {     return _screen[_cursor];  }  

If you declare a member member function as const, the function does not allow you to modify the data members of the class. For example

Class Screen {public  :    int a;public: Screen   () {    a=0;} void ok () const {    a=1;//error}  };  

 The following example is more classic:

int A=0;const int *p=&a;//To change a modifiable----the correct const int B=100;int *p=&b; Change the----error by changing the one that cannot be modified

Summary:

The 1.const member function can access Const member variables and ordinary member variables, but cannot modify any variables . The check occurs at compile time.

 The normal function can access the const member variable and cannot be modified. (will automatically turn to const at the time of the visit)

(both member functions and variables are accessible and cannot be modified)

2.const objects can access only const member functions,

(const objects can only be accessed by const, but only by const)

The member variables of a 3.const object cannot be modified.

4.The const member function cannot call a non-const member function because non-const member functions can modify member variables

5. Ordinary member functions can access ordinary data members of non-const objects, const data members, but not arbitrary data members of Const objects . (except for this point, normal can access the const)

The 6.const member function is only for non-static member functions and cannot be used for static member functions.

The const modification of a 7.CONST member function is not only added to the function declaration (including inline functions), but also defined outside the class.

8. As a good programming style, when declaring a member function, if the member function does not modify the data member, the member function should be declared as a const member function whenever possible.

Resources:

Https://www.cnblogs.com/shenckicc/p/6847103.html

Https://www.cnblogs.com/myseasky/p/7458064.html

The const function in C + +

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.