Upgrade from C to C ++ to c ++

Source: Internet
Author: User

Upgrade from C to C ++ to c ++

 

  • Const in CIt's just a "Read-Only variable"Is not a constant in the true sense,The pointer can be used to change it, as shown below:
  1. 1 # include <stdio. h> 2 int main () 3 {4 const int a = 0; // declare that the read-only variable a is 05 int * p = (int *) &; // point a pointer to the space of a 6 * p = 100; // change the content of a's bucket 7 printf ("a = % d", ); // a is changed to 1008 return0; 9}

     


Const is a constant in the true sense in C ++, Cannot be changed by pointer
  1. 1 # include <iostream> 2 usingnamespace std; 3 int main () 4 {5 const int a = 0; // declare that the read-only variable a is 0 6 int * p = (int *) & a; // give a pointer to the space of a 7 * p = 100; 8 cout <a; 9 return0; 10}

    1 constint a = 10; 2 constint B = 20; 3 int c [a + B];

     

    In C, the above Code reports the error "cannot determine the length of the array", because in C, const is only a read-only variable, essentially a variable, with storage space, of course, an error will occur when defining the length of an array using variables. However, it can be compiled in C ++, because C ++ strengthens const, making const a constant in the true sense.
    • Difference between const and # define
    # Define only replaces common text before compilation (preprocessing) without scope check!
    1. 1 # include <iostream> 2 usingnamespace std; 3 int f () {4 cout <B <endl; 5} 6 int g () {7 # define N 3 // only use N = 3 8 constint a = 100 in this function; // you want to use a = 100 9} 10 # define B 111 int main () 12 {13 cout <N <endl; 14 f (); 15 return0; 16}

       


    In code 4th, the Error "[Error] 'B' was not declared in this scope" occurs. B is not defined. Note # When a simple text is replaced before define compilation, only replace all the items under # define, and those located on # define are not replaced. Const is handled by the compiler and has the scope check and type check.
    1. 1 # include <iostream> 2 usingnamespace std; 3 int f () {4 constint a = 100; // only the scope of a = 100 is expected to be f () in 5} 6} 7 int main () 8 {9 cout <a <endl; // error, no definition of a, because a scope is only in f () 10 return0; 11}

       


    # Define can be used # undef to limit the scope, but it is a little troublesome:
    1. 1 int g(){2 #define3 int a=10;4 #undef5 }

       

     
    • C ++ enhances function types and Parameters
    The three-object operation in C ++ can return variables (provided that a and B cannot be constants! (A <B? ) = 50; this is wrong at first glance), but not in C (three orders operator (a <B? A: B) First, judge whether the left side of the question mark is true. If it is true, execute a; otherwise, execute B)
  2. For example:
    1. 1 int main()2 {3 int a=1;4 int b=100;5 (a<b?a:b)=50;6 printf("%d %d",a,b);7 }

       

    However, the above Code in C may fail, and the three-object operator cannot return variables.

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.