tags:

views:

407

answers:

4

Hello,

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

+8  A: 

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.

Brian R. Bondy
+6  A: 

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 ..

siukurnin
Last time I ran across code that had something like const int const, GCC said, "Just how const do you want it to be?" or something to that effect.
greyfade
This answer clarified a LOT of things to me. Const has always been a problem for me to understand clearly. Thanks a lot
Edison Gustavo Muenz
+1  A: 
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;
Colin
+2  A: 

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.

Johannes Schaub - litb
Hello, I thought it would have been ok to declare const in the declaration in the header, as well as the definition in the implementation file. Why would that be a good style to have it only in the definition.
robUK
it works if you put it into both. but it is not good style. because it exposes an implementation detail: that the parameter of the function won't be changed within the definition. that's totally utterly useless to know for users of the interface, and as such does not belong into the header
Johannes Schaub - litb
robUK, note i talked only about const of the parameter itself. not cases like void f(int const* p); where another thing (the referenced object) is const.
Johannes Schaub - litb