Const Usage After function declaration, const usage

Source: Internet
Author: User

[Convert] const Usage After function declaration, const usage

We usually see some function declarations followed by a const, as shown below:

Void function () const {}

What does this const do?

Let's look at the example below in detail. The following two errors will be prompted directly before compilation.

# Include "stdafx. h "# include <iostream> using namespace std; class aa {int num; public: aa () {int B = 10; num = B ;}; void out1 () {cout <num <endl;} void out2 () const {cout <num <endl;} void out3 () const {num + = 10; // error, the const function cannot modify its data member cout <num <endl ;}; int main (int argc, _ TCHAR * argv []) {aa a1; a1.out1 (); a1.out2 (); a1.out3 (); const aa a2; a2.out1 (); // The const Member cannot access non-const functions a2.out2 (); a2.out3 (); return 0 ;}

In the Declaration and definition of class member functions,

The const function cannot modify its data members.

Const object. It cannot reference non-const member functions.

Reproduced in: http://www.cnblogs.com/xing901022/p/3413019.html

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.