"03" pointer, return value, const of member function

Source: Internet
Author: User

1 //2 //2015-03-05 03/553 //const of pointers, return values, member functions4 //5 6#include <iostream>7#include <string>8 9 using namespacestd;Ten  One // ------------------------------ A //1. The const of the pointer - // ------------------------------ - Const Char*P1 ="Hello";//P is pointing to a constant the Char*ConstP2 ="Hello";//p itself is a constant - Const Char*ConstP3 ="Hello";//p itself is a constant, pointing to a constant -  - // ------------------------------ + //2. Const of the return value - // ------------------------------ + stringfoo () A { at     return "Hello"; - } -  - intMainintargcChar*argv[]) - { -     //The following is legal, changing the declaration of Foo to const string foo () in     //is no longer legal, declaring the return as const avoids the ' = = ' error as ' = '.  -Foo () ="ABC"; to     return 0; + } -  the // ------------------------------ * //3. Const for member functions $ // ------------------------------Panax Notoginseng classBar - { the  Public: +     voidfunc () A{cout <<"non-const func is called"; } the  +     voidFunc ()Const -{cout <<"The const Func is called"; } $  $     //The const property of member functions can be overloaded, and we know that non-static member functions imply this pointer, -     //All const and NON-CONST member functions can be understood as the following declaration: -     //void func (bar *this); (1) the     //void func (const bar *this); (2) -     //a Non-const object that can call (1) & (2), and a const object can only be called (2). Wuyi }; the  - intMainintargcChar*argv[]) Wu { -     //This is a bar* type and can be assigned to const . About     //follow the Non-const-const match call, if no $     //define the Non-const version, then you can call the Const.  - Bar B; - B.func (); -  A     //only the const version is called, this is the const bar* type.  +     ConstBar CB; the Cb.func (); - } $  the // ---------------------------------------------- the //4. Code redundancy due to const overloading of member functions the // ---------------------------------------------- the classTextBlock - { in  Public: theTextBlock (): Text ("Hello") the     {} About  the     //Const version the     Const Char&operator[](Constsize_t POS)Const the     { +cout <<"The const operator[] is called"<<Endl; -         returnText[pos]; the     }Bayi  the     //version Non-const can call the const version the     Char&operator[](Constsize_t POS) -     { -cout <<"Non-const operator[] is called"<<Endl; the         returnconst_cast<Char&>( thestatic_cast<ConstTextblock&> (* This) [POS] the             ); the     } - Private: the     stringtext; the }; the 94 intMainintargcChar*argv[]) the { the TextBlock TB; thetb[1] ='J';//OK Call Non-const98      About     ConstTextBlock CTB; -cout << ctb[1] <<Endl;101 102     //The Error return type is a const reference and cannot be assigned a value. 103     //in fact, if you simply return the simple variable char f (), you cannot assign a value. 104ctb[1] ='H'; the }106 107 // ----------------------------------------------108 //5. Use of mutable109 // ---------------------------------------------- the classTextBlock111 { the  Public:113TextBlock (): Text ("Hello"), Lengthvalid (false) the     {} the  thesize_t Length ()Const   //here size_t is a simple type and returns without setting a const. 117     {118         if(!lengthvalid)119         { -TextLength =strlen (text);121Lengthvalid =true;122         }123         returntextLength;124     } the Private:126     Char*text;127mutableBOOLLengthvalid; - mutable size_t textLength;129};

"03" pointer, return value, const of member 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.