Const in OBJECTIVE-C, extern,static

Source: Internet
Author: User



A. Const



1> for const, remember the key point, it just modifies the right variable.



For example:


-(void) viewDidLoad {
     [super viewDidLoad];
     // Two uses of const
     // const: decorate the basic variable p
     // These two ways of writing are the same, const only modifies the basic variable b on the right
     const int b = 20; // b: read-only variable
     int const b = 20; // b: read-only variable

     // const modifies the memory space accessed by the pointer variable, which modifies the right side * p1,
     // same way
     const int * p1; // * p1: constant p1: variable
     int const * p1; // * p1: constant p1: variable

     // const modified pointer variable p1
     int * const p1; // * p1: variable p1: constant

     // The first const modifier * p1 The second const modifier p1
     // same way
     const int * const p1; // * p1: constant p1: constant
     int const * const p1; // * p1: constant p1: constant
}


2> Application Scenarios for const


// If I need a method, the parameter is the address, only the value can be obtained by the address, and the address cannot be modified

// * a read-only cannot be modified * a
-(void) test: (const int *) a {
     a = 20;
}

// If you cannot modify the value of a, you can only modify the value accessed by a
// a read only cannot modify a
-(void) test1: (int * const) a {
     * a = 20;
} 


The difference between 3>CONST and macros



(1) A macro is a precompiled execution, and a const is a compiled execution.



(2) The macro does not compile the check, it just does the substitution, therefore does not compile the error, the const compiles the check, compiles the error.



(3) Macros can define methods or functions, and Const cannot.



(4) Extensive use of macros can cause long compilation times.



(5) Note: A large number of use of macros will not consume a lot of memory, macros are defined as constants, constants are stored in constants, only generate a memory, not as some people say will consume a lot of memory!



Two. Static and extern



1>static Effect:



(1) Modifying local variables



1) Extend the declaration period of the local variable, and the program ends before it is destroyed.



2) Local variables are initialized only once, saving memory



3) Change scope



(2) Modifying global variables



1) can only be accessed in this file, modify the scope of global variables, the life cycle does not change



2) Avoid repeating the definition of global variables



2>extern Effect:



(1) is only used to get the value of the global variable, cannot be used to define the variable



(2) Working principle is to find in this file there are no global variables, not found, then go to other files to find.



Three. Use with static and const



1> preventing duplicate declaration of global variables



2> does not want colleagues to inadvertently modify variables, allowing only read



3> declares a static global read-only constant



Four. extern is used in conjunction with const



1> often uses the same string constant in multiple files, using the combination of extern and const



2> generally build a globeconst



Const in OBJECTIVE-C, extern,static


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.