Const and this

Source: Internet
Author: User

Const

1. Basic usage

I constant Declaration

 
1 Const DoublePi =3.1415926;
2 Const IntAQ =10;
3 IntQ =5;
4 Const Int* P = & Q;

To put it simplyProgramThe value that remains unchanged in it. It will be protected after being declared using const!

II Functions

1. Void fuction1 (const int R );//The parameter is passed here.ConstValue, meaning that the initial value of the variable cannot be changed by the function

2. Const int fuction1 (INT );// Return here Const Value, which means that the initial values of the returned original function variables cannot be modified, but the variable returned by the function by value is made a copy. It makes no sense if the variable can be modified, it can be assigned to any Const Or not Const Type Variable. You do not need to add this Const Keyword. However, this is only for the internal type (because the internal Type Returns a value without returning a variable and is not used as the left value). For the user-defined type, the return value is a constant. For more information, see the following. 3 .

3.Class CX ;//There are constructor internally. The declaration is as follows:CX (INT r = 0)

CXFuction1 () {return Cx ();}

Const CX fuction2 () {return Cx ();}

As shown in the preceding custom class CX , And functions Fuction1 () And Fuction2 (), When we perform the following operations:

Fuction1 () = Cx (1 );//No problem. It can be called as the left value.

Fuction2 () = Cx (1 );// Compilation error, Const The return value cannot be called as the left value. Because the left value regards the return value as a variable and modifies its return value, Const This modification is forbidden.

4. PointerConstTransfer and return:

Int F1 (const char * pstr );// Used for Transmission Const Modification ensures that the initial value of the passed parameter is not modified through this pointer. Any modifications are made within the function. * Pstr Will cause compilation errors.

Const char * F2 ();// It means that the pointer returned by the function points to an object Const Object, which must be assigned to Const Object Pointer.

Const char * const F3 ();// One more Const , This Const The pointer is valid only when it is used as the left value. Const The object itself cannot be modified, so it cannot be processed as the left value.

5.TheConstTransfer:

Void F1 (const X & px );//SuchConstThe effect of passing a reference is exactly the same as that of passing the most common function by value. It prohibits all modifications to the referenced object, the only difference is that a copy of the class object will be created for passing by value first, and then passed in, and it will directly pass the address, so this transfer is more effective than passing by value.

** In addition, only the referenced Const You can pass a temporary object because the temporary object is Const And is invisible. It exists in a local domain for a short time, so no pointer can be used, only the referenced Const Pass to capture this guy.

1 VoidCcube: Say ()Const
2{
3Cout <"This is just four-sided shouting."<Endl;
4
5}

This

Basic usage

I personally think that the significance of this lies in the fact that after the instantiation of a class

A reference:

When you enter a house,

You can see tables, chairs, floors, etc,

But you cannot see the whole picture of the house.

For an instance of a class,

You can see its member functions and member variables,

But what about the instance itself?

This is a pointer that always points to your instance.

Memory reality: We recommend a book for this look can: http://115.com/file/dnrb7g66

Reference: http://blog.csdn.net/jsjwql/article/details/1779516

 

 

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.