Const function Summary (Author: mingyuanonline # gmail.com)

Source: Internet
Author: User
/* Const function Summary (Author: mingyuanonline # gmail.com)
* To declare a const member function, you only need to add the const keyword between the function parameter table and the function body. (This sentence has been changed. For the original version, see the original author's blog)
* Note: we can only do this for class member functions, but not for general global functions.
* When a function is a class member, it is meaningful to declare it as a const. Its function is to make this pointer in the function a const,
* This means that we cannot write the data member of the class on the left of the value assignment statement defined in the function-it will be marked as an error by the compiler.
* Const member functions cannot call similar non-const member functions, because they may also modify the current object.
* After an object is declared as const, all member functions that can be called by this object must be const. Otherwise, the program cannot be compiled.
* Note: above reference http://blog.csdn.net/lidh04/archive/2008/07/27/2720309.aspx
*
* Summary:
* 0. Declare the const member function. You only need to add the const keyword between the function parameter table and the function body.
* 1. The const function declaration only makes sense in the class and can only appear in the class. It serves as a member function of the class.
* 2. the const member function can only call the const member function.
* 3. Non-const functions can call the const member function.
* 4. Non-const objects can call the const member function.
* 5. The const object can only call the const member function.
* 6. The const object can access static member functions.
*/
# Include
# Include
Using namespace STD;
Class class_const_member_function
{
Public:
Class_const_member_function (string name = "No Name"): _ name (name ){};
String display () {return _ name;} // non-const member function
Void display_const (string name) const {cout // void const_try_to_modify_member_variable () const {_ name = "99" ;}// an error occurred when the const member function tried to modify the member variable!
Void const_call_const () const {display_const ("const_call_const") ;}// the const member function calls the const member function
// Void const_call_nono_const_function () const {display () ;}// const function call non-const, Error!
Void nono_const_call_const () {display_const ("nono_const_call_const") ;}// a non-const member function calls the const member function
Static void print (string whos) {coutprivate:
String _ name;
Static string _ school;
};
String class_const_member_function: _ school ("school ");
Void const_member_function_test ()
{
Class_const_member_function MEM; // declare a non-const object
Cout mem. display_const ("name"); // non-const object calls the const member function
Mem. Print ("mem ");

Const class_const_member_function const_mem; // declare a const object
Const_mem.display_const ("const_name"); // The const object calls the const member function.
// Const_mem.display (); // The const object calls a non-const member function. An error occurred!
Const_mem.print ("const_mem"); // The const object accesses the static member function.

Class_const_member_function: Print ("class"); // The class name calls the static 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.