Const usage in C ++

Source: Internet
Author: User

Is const of java the same as that of C ++? How to implement it?

 Const and goto are only reserved words in Java and are not implemented in use. Their differences are as follows:
(1) Final defines constants in Java and can act on basic types or class types. if it acts on class types, such types cannot be inherited as parent classes, that is to say, there cannot be subclasses under it. Such classes are called atomic classes.
Const in C ++ defines Constants
(2) If final in Java is for the basic type, it is the same as const in C ++. If it is for the object, it is different.
(3) Final indicates that the handle cannot be changed.
Final object OBJ = (object) New String ("");
OBJ = (object) New String ("hello"); is invalid
However, you can still call the OBJ method. For example, (string) OBJ). Length () is legal
If an object is defined as const, C ++ cannot call the object method. Unless this method is defined as const

 

1.ConstConstant, suchConst int max = 100;
Advantages:ConstConstants have data types, while macro constants do not. The compiler can perform a type security check on the former, while the latter only performs character replacement without the type security check. In addition, unexpected errors may occur during character replacement.

2.ConstModify the data member of the class.

Const data members are constants only within the lifetime of an object, but they are variable for the entire class. Because the class can create multiple objects, the values of the const data members of different objects can be different. Therefore, the const data member cannot be initialized in the class declaration, because the compiler does not know the value of the const data member when the class object is not created. Const data member initialization can only be performed in the class constructor initialization table.

3. const to modify the pointer, see the following formula:
const int * A = & [1]
int const * A = & [2]
int * const A = & [3]
const int * const A = & [4]
For more information, see valid tive C ++ item21 , if const is on the left side of the asterisk, const is used to modify the variable to which the pointer points, that is, the pointer points to a constant. If con St is on the right side of the asterisk. const modifies the pointer itself, that is, the pointer itself is a constant. Therefore, [1] and [2] are the same, the content pointed to by the pointer is a constant. In this case, you cannot change the content, if not, * A = 3 ; [3] indicates that the pointer itself is a constant, and the content pointed to by the pointer is not a constant, in this case, the pointer itself cannot be changed. For example, A ++ is incorrect; [4] indicates that the pointer itself and the content it points to are constants.

4. const initialization
let's take a look at const variable initialization
1) non-pointer const constant initialization: a B;
const A = B;
2) pointer const constant initialization:
A * D = new A ();
const A * c = D;
or: const A * c = new ();
3 ) events that reference const constant initialization:
a f;
const A & E = F; // E only access declared as const / span> function, but cannot access common member functions.

5. const application in function declaration / p>

 In the function declaration,ConstYou can modify the return value of a function or a parameter. For a member function, you can also modify the entire function.1) Modify the ParameterConst, SuchVoid fun0 (const A * A); void fun1 (const A & );When you call a function, use the corresponding variable to initialize the const constant. In the function body, perform the constant operation according to the Section modified by the const. For example, if the parameter is const A *,The content of the passed pointer cannot be changed to protect the content pointed to by the original pointer.; If the parameter is const A &,The passed referenced object cannot be changed to protect the attributes of the original object..

[ Note: ] : Parameter Const It is usually used when the parameter is a pointer or reference, and can only modify the input parameter ; If the input parameter uses the "value transfer" method, because the function will automatically generate a temporary variable for copying this parameter, this parameter does not need to be protected, so you do not need to use the const Modify.  
[ Summary ] For non-Internal data type input parameters " Value Transfer " Method changed "Const Reference Transfer " To improve efficiency. For example Void func () Change Void func (const A &)
Do not set " Value Transfer " Method changed "Const Reference Transfer " . Otherwise, the function can not improve the efficiency, but also reduce the comprehensibility of the function. For example Void func (int x) Should not be changed Void func (const Int & X)
2 ) Modifies the returned value Const , Such Const A fun2 (); const A * fun3 ();
After the return value is declared, Const Follow " Modification principles " To provide protection.

[Summary] 1) When the return value of a function is an objectConstIs mostly used for operator overloading. Generally, it is not recommended to useConstWhen the type of the return value of a modifier is an object or an object is referenced.2) If"Pointer Transmission"Method function return value plusConstThe content of the function return value (that is, pointer) cannot be modified. The return value can only be assignedConstThe same type of pointer.

6.In the class member functionConstUse 
Generally placed behind the function body, such:Void fun () const;
Any function that does not modify the data member is declaredConstType. If you are writingConstWhen a member function is called, the data member is accidentally modified or other non-ConstMember functions, the compiler will report errors, which greatly improvesProgramRobustness.

7. UseConstSome Suggestions 
1Be boldConstThis will bring you endless benefits, but the premise is that you must understand the original committee; 
2To avoid the most common assignment operation errors, for exampleConstAssign values to variables. For details, see questions; 
3Use in ParametersConstA reference or pointer should be used instead of an ordinary object instance, for the same reason; 
4 constThree methods of member functions (parameters, return values, and functions) should be well used; 
5Do not set the function return value typeConst;
6Except for overload operators, do not set the return value typeConstReference;

 

 

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.