D
Denis Remezov
johny said:Can someone please explain to me the difference between these two:
function1( const int a)
function2( int const a)
Both seemed to compile, but what is the difference between the two above.
And why would one choose one over the other.
"const int a" and "int const a" are technically the same. In general, the
former pattern is more popular, the latter has arguably a more consistent
behaviour (some may disagree). Here is an illustration of what I mean:
Assume expression EXPR represents some type. Then EXPR const (where it is
allowed) will always have the same meaning: constant EXPR. The meaning of
const EXPR, however, will depend on what EXPR is:
typedef any_type_name EXPR;
//fine: const EXPR is the same as EXPR const
#define EXPR type_name*
//const EXPR or, directly, const type_name* now means something
different: a pointer-to-const data, not a const pointer-to-data.
By the way, a const modifier on the type of a function parameter
doesn't do anything except for preventing the function from
modifying its /own copy/ of the argument. It doesn't make sense
to use it here.
a is a pointer to constant data (it would not let you change the pointed-tomoreover, I thought there was a difference between the two below where one
would not let the value change whereas the other one would not let the
address of the pointer change, but I must be missing something here.
function3( const int* a)
value); everything is fine.
a is a constant pointer to data; it would not let you change the /value/function4( int* const a )
of the pointer a (the address of a variable never changes). This const is
rather, sorry, pointless, as in the case of function1 and function2.
Denis