After the const is placed in the function

Source: Internet
Author: User
The const is placed after the function to indicate that the function is a constant member function, and the constant member function is a function that cannot change the value of the member variable.
The const qualifier, which converts an object to a constant.
Example:
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); That's right

After the const is placed in the function

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.