C ++ const

Source: Internet
Author: User

# Include <iostream>
Using namespace STD;
Class AA
{
 
Public:
AA (int s)
{
X = s;
}
Void setx (INT) const;
Void pp () const;
PRIVATE:
Int X;
 
 
 
};
Void AA: setx (INT s) const // The constant function cannot modify the member.
{
// X = s;
}
Void AA: pp () const
{
Cout <x <Endl;
}
Int main ()
{
Const aa a (3); // The const object can only call the const member function, so that the Member will not be modified illegally.
// A. setx (5 );
A. PP ();
Return 0;
}
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.

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.