简体   繁体   中英

using const to prevent datatype changing and value changing

Is there a difference between using const:

Cannot change the datatype but can change the value of a or b

int add(const int a, const int b);

Can change the datatype but cannot change the value of a or b

int add(int const a, int const b);

Cannot change the datatype and cannot change the value of a or b

int add(const int const a, const int const b);

Many thanks for any suggestions

Difference between const int and int const:

int const and const int are the same.

There is a difference with pointers though:

char sz[3] = "hi";

//const char* allows you to change what is pointed to,
//but not change the memory at the address that is pointed to
const char *p = sz;
p = "pi";//ok
//p[0] = 'p';//not valid, bad

//char * const allows you to change the memory at the address that is 
//pointed to, but not change what is pointed to.
char * const q = sz;
//q = "pi";//not valid, bad
q[0] = 'p';//ok

//or disallow both:
const char * const r = sz;
//r = "pi";//not valid, bad
//r[0] = 'p';//not valid, bad

Most of the time you want to use const char *.

Changing the type of a varaible:

You cannot change the type of a variable, but you can re-interpret the address of a variable to be of another type. To do this you use casting.

I don't know how one is supposed to changed the datatype of a variable in C++...

'const' is a promise you make to the compiler about not modifying a value. It complains when you don't (probably uncovering z bug in the process). It also helps it to do various optimizations.

Here are some const examples and what they mean:

f ( const int a  )

f cannot change the value of 'a'.

f ( int const a )

the same but written in a weird way

f ( const int const a )

means nothing, gcc tells me "duplicate const"

f ( const int * pa )

f cannot change the value pointed to by pa

f ( int * const pa )

f cannot change the value of the pointer

f ( const int * const pa )

f cannot change the value of the pointer nor the value pointed to

f ( int a ) const 

The member function f cannot modify its object

Hope it makes things clearer..

You can never change the data-type of any variable. If you have const int it is the same as int const always. Though, for function declarations, there are special cases.

Actually,

int add(const int a, const int b);

and

int add(int a, int b);

Or any combination of const in it all declare the same function. To outside, they are all the same and have actually also all the same type. It only matters for definition of functions. If you don't put const

int add(int a, int b) { a++; /* possible, increment the parameter */ }

you can change the parameters (which in this example are copies of the arguments passed). But if you put const, parameters will be const within the function definition

int add(int const a, int const b) {
    a++; // bug, a is a constant integer!
}

Why does it not matter whether you write const or not for function declarations? Because the argument will be copied, and so it will not have any impact on the caller and the callers arguments anyway, For that reason. the following style is recommended, In a header, declare the functions without const

int add(int a, int b);

Then, in the definition, if you want a parameter to be const, put the const in.

#include "add.hpp"

// remember, const int and int const is the same. we could have written
// int add(const int a, const int b); too
int add(int const a, int const b) { return a + b; }

Same counts for member functions

struct foo {
    void f(int);
};

void foo::f(int const a) { ... }

Note that we only talked about the const that affects the constness of the parameter directly. There are other const that affect constness when using references or pointers. Those consts are not to be ignored and are actually important.

const int x;

is the same as

int const x;

The order of keywords is not relevant. This holds true for keywords like unsigned as well:

const unsigned int x;

int unsigned const x;

This rule doesn't apply the same way with pointers, though, as an asterisk (*) is not a keyword, it's an operator. So the previous rule doesn't apply:

const int *x;

Is not the same as:

int * const x;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM