C + +: C + + function return type what happens with a const

Source: Internet
Author: User
Tags getv

The return type of C + + functions, including when does const work?

    • The function return value does not want it to be modified immediately.

As an example, this is a simple way to avoid the creation of the invisible return variable, the function return of ABC is a reference, the 10th line in the main function, the + + operation is based on the const int & type, so there will be an error, but the subsequent operation of the reference will not be subject to the const constraint.

The advantage of this is to avoid the function return value and the operator's logic error, such as the following example, the function returned by the + +, for the main function is not intuitive, further application is in the operator overload, see the next case description.

1 Const int& ABC (intAintBint&re)2 {3Re = A +b;4     returnre;5 }6 7 intMain ()8 {9     intA =1, B =2, C;TenABC (A,B,C) + +; Error OneC + +; That's right Acout<<c<<Endl; -     return 0; -}

    • Overloaded operators conform to logical

General variable assignment A=b=c, this is no problem, but (a=b) =c Compile error, this is the judgment of the built-in operator, if the = operator is overloaded, in order to avoid such a logic error, you need to add a const constraint on the return type, in the following example, the Class A overloaded operator +, If the return type in line 6th does not have a const constraint, then 15 rows of compilation will pass, if the scenario is: if (a+b==c) is mistakenly written as if (A+b=c)

1 classA2 {3  Public:4     intA;5Aintb): A (b) {}6FriendConstAoperator+(Consta& LfT,Consta&RGT)7     {8         returnA (LFT.A +rgt.a);9     }Ten }; One  A intMain () - { -A A (1), B (3), C (7); theA+b =C;/////////error -     return 0; -}

    • Create a pointer to a constant by using a function

If you create a constant string through a function, in addition to the constraint in the main function, you can constrain in the function return type, the const constraint on the left of the first row is the index of the pointer that returns a constant string, because it exists 12 rows must be declared as const char *p, if the first row is left const Does not exist, then 12 rows can be added without const, so that the effect of the constraint string is only reflected in main, not very good to express the function of calling the function.

The second const in the first row is also invalid, and it is constrained by a hidden pointer that is converted from char * p to char * const, and the pointer value of the hidden pointer is given to p in the main function, so the latter can be modified to point to. (At this point my understanding is not OK or correct)

1 Const Char*ConstHelpfun ()2 {3     Char* p =New Char[3];4p[0]='a';5p[1]='b';6p[2]=' /';7     returnp;8 }9 Ten intMain () One { A     Const Char* p =Helpfun (); -p++; -cout<<p<<Endl; the delete p; -     return 0; -}

    • Satisfies a call to a const member function

This combination of examples is relatively easy, here is the main reason is:const type of object, cannot call its own non-const member function, but can call its own const member function . For example, the following example, B is declared as const A, 12 rows can be compiled successfully, 13 rows are missing error, the root cause may be internal this pointer conversion (indeterminate).

1 classA {2  Public:3A (): Num (2) { }4     voidSetnum () {}5     voidGetnum ()Const{ }6 Private:7     intnum;8 };9 intMain ()Ten { One     ConstA B; A b.getnum (); - b.setnum ();///////////////////////error -     return 0; the}

Based on one of the reasons above, it is easy to show that Class A is a hidden variable generated in class B operations, and in order to invoke the Const function in Class A, the Class B production function return type needs to be const.

1 classA2 {3  Public:4A (): Num (2){}5     voidSetnum () {num =Ten; }6     voidGetnum ()Const{7printf"%d\\n", num);8     }9 Private:Ten     intnum; One }; A  - classB - { the  Public: -     ConstA *Get() -     { -A *p =NewA (); +         returnp; -     } + }; A  at intMain () - { - b b; -B.Get()getnum (); -B.Get()setnum (); -     return 0; in}

    • The return type of the const member function is quoted when you need to add a const constraint
int Const ; int Const;

The member function above is legal, the following is not legal, specific examples are as follows, for the 5th line function return type of the const cannot be omitted, regardless of whether the 10th line has a const, this should be the internal value object type conversion, the value of the GetValue function is converted to a const Int (the 10th line does not have a const), but if the int & type of the return is a const int, int & This type conversion, which is forbidden, the return reference must be const, in summary, if Getvalu If e is a const function and the return type is a reference, then the const in the return type and 16 of the rows must not be omitted.

1 classTest2 {3  Public :4Test (inta): value (a) {}5     Const int& GetValue ()Const6     {7         returnvalue;8     }9 Private:Ten     Const intvalue; One }; A  - intMain () - { theTest T (3); -     Const int&a =T.getvalue (); -cout<<a<<Endl; -     return 0; +}

Extension if the const member function returns the value of the pointer? This is the same as the return int type is assigned to the accepted object, and the reference must have a const because there is no replication, for example, the following code can be compiled to run, and also found to bypass the private constraints of the class. The 6th line returns the type plus const, which is actually the third case. If line 13th is a const int * p, then the situation is different, and the 6th and 19th lines need to be const.

1 classTest2 {3  Public :4Test (intA): Value (a) {p = &value;}5     voidSetValue () {value =Ten;}6     int* GetValue ()Const7     {8         returnp;9     }Ten     intGetv () {returnvalue;} One Private: A     intvalue; -     int*p; - }; the  - intMain () - { -Test T (3); +     int*a =T.getvalue (); -cout<<*a<<Endl; +*a =5; ACout<<t.getv () <<Endl; at delete A; -     return 0; -}

Resources:

Http://www.docin.com/p-97354417.html

Http://blog.chinaunix.net/uid-24922718-id-3480107.html

Http://blog.sina.com.cn/s/blog_4366aa320100cknr.html

http://blog.csdn.net/zhjxin1800/article/details/7584375

Http://www.cnblogs.com/lichkingct/archive/2009/04/21/1440848.html

C + +: C + + function return type what happens with a const

Related Article

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.