const in c and c ++ application in

const in c and c ++ application in

const definition : const is a keyword in ANSI C, which allow to change defining a variable, generating static effect. Use const to a certain extent can improve the safety and reliability of the program, const also appear in other programming languages, such as Pascal, C ++, PHP5, BC # and so on.

A .const in c and c ++ in Basic usage

  • const is used to define a variable, when the int const a = 10; At this time variable is a read-only rights. Then a read-only, it can not be left value that can not be assigned.
    Note that at this time const int a = 10; and int const a = 10; the same semantics. When modifying a const variable, be sure to give this variable is initialized, if not initialized, in the back can not be initialized.

    #include<iostream>
    using namespace std;
    const int a=10;
    int main(){
    int b=a; //不为左值 合法
    a=100 //为左值 非法

  • In some c language compiler (such as vc ++ 6.0) const variables used to initialize the array, the compiler will complain. This is due to "read-only variable" and "constant" of. But note that in C ++ you can, because const been expanded meaning.

   However, the number and the type defined by the enum #define macro can be used to define a constant initial transducer array.

two. Const and the difference between #define

   Two of them from the functions that they like, but they have different.

  • define pre-compiled instructions, const common variable is defined. const variables are defined, but the definition is #define constants. #define macro is defined in the pre-deployment phase, and read-only variable const definition is used in compiling the operational phase. For example, to initialize an array of just using #define will be able to run.

  • Since the compiler constants are placed in the read-only area of memory, of course, will not be able to modify it. The "read-only variable" is to open up a new memory to hold its value, but this value is defined by the compiler can not be modified. The ANSI C definition of the length of the specified array must be "constant", so the error.
    But in fact not as a length of the array in addition to an advantage by the conventional macro variables defined const and more convenient to use. Therefore, the const can programming and define possible to make use of a case where const.

three. Use const pointer with one-dimensional

  • int const P1 = & A; equivalent to const int P1 = & A;

  • int const p1 = & A; // const modified p1, p1 will be left as a legitimate value, the p1 value as an illegal left
    we can see, const is modified
    p1, * p1 value can not be changed directly, but can be changed to point to change value p1.

  • int * const p2 = & a; // const modified p2, p2 will be left as illegal value, the p2 value is left as legitimate
    in this case, const is modified p2, p2 is not directly change direction, but can change
    the value of p2 .

  • int const const p3 = & A; // const modified respectively p3, p3, will p3, p2 values are left as illegal, then p3 p3 values and direction is not to be changed
    , we can see, const is modified p1, not directly change the value of p1, but can be changed to point to change the value p1.

  Summary: what is behind const, what can not be modified. Int Eg.const P1 = & A; and const int & p2 = A; * p1 respectively, and the values can not be changed to point p2.

four. const with function

  const greater role is function of the parameters that can be modified, return values, and even the function definition body. But note that if the output parameters need to do with const lost function.

  • E.g:

void StringCopy(char*strDestination, const char *strSource);

 Wherein strSource is an input parameter, strDestination are output parameters. After adding const modified to strSource, if the function of the body's attempt to modify the contents of the statement strSource, the compiler will point out the error.

  • When the delivery address const qualifier, the return value of the function can be assigned to the same type const qualifier pointer, or using casts.

const int* func(void){ //函数声明
int* a = func(); //报错
int* c = const_cast<int*>(func()); //正确
const int* b = func(); }//正确

(3) const member functions
without modifying the data members function as much as possible declared const type. If at the time of writing const member functions, accidentally modify the data members, or by calling the other non-const member functions, the compiler will point out the error.

Guess you like

Origin www.cnblogs.com/xqy-888/p/11515485.html