Const meaning

xiaoxiao2021-03-06  13

Declaration constant

Const int A = 0;

A = 5; // error

2. Modification function row, guarantee that the line is not modified in the function

Void Fn (INT I)

{

i = 5; // error

}

3. Modify the member function, enable member functions to call member variables, and do not change when used

Class A

{

INT I;

INT f () const {returni}

}

-------------------------------------------------- -------------

Const Declaration

MEMBER-FUNCTION ConST

When modifying a data declaration, the const keyword specifies that the object or variable is not modifiable. When following a member function's parameter list, the const keyword specifies that the function does not modify the object for which it is invoked.

//

Constant Values

The Const Keyword Specifies That A Variable's Value Is Constant and Tells The Compiler To Prevent The Programmer from Modifying IT.

Const Int i = 5;

i = 10; // error

i ; // error

In C , you can use the const keyword instead of the #define preprocessor directive to define constant values. Values ​​defined with const are subject to type checking, and can be used in place of constant expressions. In C , you can specify the size of An Array With a const variable as stock:

Const int maxaRray = 255;

Char store_char [maxArray]; // Legal In C ; Illegal In C

In C, Constant Values ​​Default To External Linkage, So They Can Appear Only In Source Files. IN C , Constant Values ​​Default To Internal Linkage, Which Allows The Appear in Header Files.

The Const Keyword Can Also Be Used in Pointer Declarations.

Char * const aptr = mybuf; // constant Pointer

* APTR = 'a'; // legalaptr = Yourbuf; // Error

A Pointer to a variable declared as const can be assigned Only to a Pointer That Is Also Declared As Const.

const char * bptr = mybuf; // Pointer to Constant Data

* BPTR = 'a'; // Error

Bptr = Yourbuf; // Legal

You can Use Pointers to Constant Data As Function Parameters to Prevent The Function from Modifying a parameter Passed THROUGH A POINTER.

You Can Call Constant Member Functions Only for a constant Object. This Ensures That The Object is never modified.

Birthday.getMonth (); // okay

Birthday.setMonth (4); // error

You can call either constant or nonconstant member functions for a nonconstant object You can also overload a member function using the const keyword;. This allows a different version of the function to be called for constant and nonconstant objects.

You Cannot Declare Constructionors or Destructors with the const keyword.

//

Constant Member Functions

C Specific

Declaring a Member Function with The const keyword specifies That The Function is a "read-only" Function That Does Not Modify The Object for Which It is Called.

To declare a constant member function, place the const keyword after the closing parenthesis of the argument list. The const keyword is required in both the declaration and the definition. A constant member function can not modify any data members or call any member functions that aren ' T Constant.

END C Specific

EXAMPLE

// EXAMPLE OF A Constant MEMBER FUNCTION

Class Date

{

PUBLIC:

Date (int Mn, Int Dy, int yr);

INT getMonth () const; // a read-only function

Void SetMonth; // a Write function;

// cannot be const

Private:

Int Month;

}

INT DATE:: getMonth () const

{

Return Month; // Doesn't modify Anything

}

Void Date :: SetMonth (int MN)

{

Month = mn; // modifies Data Member

}

-------------------------------------------------- -------------

#define max 100 / * C language Macroembox * /

Const int max = 100; // c language constant constant

Const float pi = 3.14159; // C language constant constant

Comparison of Const and #define

The C language can define constants with const, or use #define to define constants. But the former is later

(1) The constant constant has a data type, and the macro regular amount has no data type. The compiler can perform type security checking for the former. For the latter only characters, there is no type of security check, and it may generate unexpected errors (marginal effects) in characters.

(2) Some integrated debugging tools can debug a Const constant, but cannot debug a macroemption

If the parameter is a pointer, it is only used for input, then CONST should be added before the type.

The pointer is accidentally modified in the function.

Void stringcopy (char * strdestination, const char * strsource);

If the input parameter passes the object in a value passed, "Const &" mode should be used.

To pass, this can save the construction and secting process of temporary objects, thereby increasing efficiency.

//

Improve the robustness of functions with const

Seeing the const keyword, the C programmer first thinks of the constant constant. This is not a good condition reflection. If you only know the constant defined with const, it is equivalent to only use gunpowder to make firecrackers. Const greater charm is that it can modify the parameters, return values, or even function of the function.

Const is Constant's abbreviation, "constant" means. Things that are modified by const are mandatory, prevent accidents, can improve the robustness of the program. So many C program design books: "Use constly you need"

-------------------------------------------------- -------------

The char const * PC and COSNT Char * PC are the same.

When Const is modified, there will be five combinations:

1.const char * pc; 2.char const * pc;

3.char * const pc;

4.const char * const pc;

5.char const * const pc;

Interpretation method:

Method 1: (This method is the simplest, recommended)

A. First find the "*" number;

B. "*" Has "const" on the left side, indicating that the object points to the pointer is const type.

C. There is "const" on the right side of "*", indicating that the pointer itself is const type.

D. "*" Has "const", indicating that the object points not only by the pointer is a const type, and the pointer itself is also const type. As the saying goes, "the two heads are killed";

Method Two:

A. Principle, read from the right side to the left:

B. For example: char const * const pc; the order is as follows: PC -> const -> * -> const -> char; We read: "PC" is a "const" type pointer ("*" reading ), It points to a "CONST" type "char" value.

This method does not have a "method one" fast, but the meaning of reading is complete, "method one" is used to quickly determine "const" to the end of the pointer itself or pointer to the object.

转载请注明原文地址:https://www.9cbs.com/read-46899.html

New Post(0)