The difference between C and C + + in const usage

Source: Internet
Author: User

First, C and C + + are substantially different in structure, but syntactically identical. So in the use of time, we will often encounter some puzzling problem, think grammatical is correct, but compile time but appear a red error! For example, this interesting question that I met today.

1. It's interesting to see a piece of code today:

1 #pragmaWarning (disable:4090)2 3#include <stdio.h>4 5 intMainintArgvCharargs []) {6 7  Const int var=4;8     int*p = &var;9*p =1;Tenprintf"%d\n",var); One GetChar (); A}

The answer is very definite: 1 is also simple: C + + is a weakly typed language. The P pointer points to the address of Var and changes the value of Var!

But let's take a look at this two code:

1#include <iostream>2 3 using namespacestd;4 5 intMainintargs,Charargv[]) {6 7     Const int var=Ten;8Std::cout <<"var ="<<var<<Std::endl;9     //int * p= &var; --can not be converted, will error! Ten     //You can use the cast One     int*p = (int*) &var;  A*p =3; -Std::cout <<"*p ="<< *p <<Std::endl; -     intnum[var]; the  -     inti =0; -     //Initialize -      for(Auto data:num) { +Data =++i; -std::cout<<"num["<<i-1<<"] ="<< Data <<Std::endl; +     } A GetChar (); at     return 0; -}

2. Even if you use this const_cast ": Force remove the const attribute, but *p=3, this statement, p point to the value of the address still does not change!" The values for both codes are this:

1#include <iostream>2 3 using namespacestd;4 5 intMainintargs,Charargv[]) {6 7     Const int var=Ten;8Std::cout <<"var ="<<var<<Std::endl;9     //int * p= &var; --can not be converted, will error! Ten     //You can use the force to remove the const attribute One     int*p = const_cast<int*> (&var);  A*p =3; -Std::cout <<"*p ="<< *p <<Std::endl; -Std::cout <<"var ="<<var<<Std::endl; the     intnum[var]; -  -     inti =0; -     //Initialize +      for(Auto data:num) { -Data =++i; +std::cout<<"num["<<i-1<<"] ="<< Data <<Std::endl; A     } at GetChar (); -     return 0; -}

Results

As: we can see that the value of VAR, no change! The reason is that although C + + is weak type language, but stronger than the C language characteristics, C + + data type more stringent and harsh!

2. What about pointers, then? Const uses the pointer to play a large role, you can set permissions, such as int const * P/CONST int *p (these two can actually be considered the same type) are constant pointers, and int * Const P pointer constant, and const int * const p;

The permissions according to their attributes are as follows:

The int const *P-----Can read only, cannot write, since it is a pointer to a constant, of course, it can point to other constants.

the int * Const P-----Can read and write, but since it is a pointer constant, of course, it cannot point to other addresses and cannot modify the values of other addresses.

the int const * Const p----Read and cannot be written, and cannot point to the value of another address.

This allows you to set permissions, which is safe and proper for the system, and it's power! Of course, you can use the pointer function or something!  This kind of const actually function is very useful beginning! Especially in systems with high security requirements, such as the banking system or something.

The difference between C and C + + in const usage

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.