c ++ and c const in const difference

#include <algorithm> 
#include <iostream> 
#include <Functional> 
#include <the Vector> 
#include <numeric> 
#include <Array> 
#include <CString> 
#include <cstdio> 
#include <Functional> // Packaging header 
the using  namespace   STD;
 #if 0
 // in C 
int main () 
{ 
    const  int NUM = 100 ;
     // int A [NUM]; // error, num is an artificial constant 
    * ( int *) &a = 4 ;
    printf ( " % d " , NUM); // 4, C language, const is not a true constant in the sense of, can only avoid direct modification can not be avoided indirectly modify 
    getchar (); 
} 
#endif 
// in c ++ in 
int MAIN1 ( ) 
{ 
    // const int n = 20 is;
     // int a [n]; // can, because c ++ compiler will automatically do the optimization, where n was found to be 10 will directly replaced, rather than to take the memory 
    
    int a = 10 ;
     const  int n-= A; // const pseudo remains constant, but only c ++ compiler optimization will do, but here is a variable, 
    int Data [n-]; // not .c ++ compiler optimization not random, because variable may change 

} 
void main3 () 
{ 
    const  int NUM =20 ;
     * ( int *) (& NUM) = . 3 ; 
    COUT << * (& NUM) << endl; // optimization, forced replacement does not take from the memory, and taken directly from register 20 
    cout << num << endl ; // 20 is 

} 


void main4 () 
{ 
    int   A = 10 ;
     const  int NUM = A; // not directly optimize 
    * ( int *) (& NUM) = . 3 ; 
    COUT << * (& NUM) << endl; / / taken directly from memory 10, the variables not directly optimize 
    COUT NUM << << endl;

}

void main()
{
    const intNUM [ . 5 ] { . 1 , 2 , . 3 , . 4 , . 5 };
     const  int * = P NUM;
     * ( int *) P = 100 ; // but * p = 10, the constant pointer can not be modified
     // const values are not optimized, may be indirectly changed 
    for (Auto I: NUM) 
    { 
        COUT << I << endl; // 100. . . . . . . 
    } 

}

 

Guess you like

Origin www.cnblogs.com/bwbfight/p/11299511.html