Tags: const, function, global, int, microsoft, msdn, parameter, prototype, software, value, visual
const + & on the same prototype
On Microsoft » Microsoft Visual C & C++
1,373 words with 2 Comments; publish: Mon, 02 Jun 2008 10:28:00 GMT; (32546.88, « »)
int add (const int &);
This function can't change the parameter value. But able to change its
global value. Right?
Thanks
Jack
http://visual-c.itags.org/q_visual-c_62623.html
All Comments
Leave a comment...
- 2 Comments

- Jack schrieb:
> int add (const int &);
> This function can't change the parameter value.
Right.
add() gets a reference to a constant int (read backwards).
> But able to change its global value. Right?
What does that mean?
Like this ?
# file.cpp
int foo = 3;
int add(const int& n)
{
n = 3; // compile error
foo = 3; // OK
return n;
}
//...
int x = add(foo);
/S
--
Stefan Naewe
naewe.s_AT_atlas_DOT_de
#1; Mon, 02 Jun 2008 10:29:00 GMT

- "Stefan Näwe" <please.visual-c.itags.org.nospam.net> '?:drju4e-9p2.ln.visual-c.itags.org.news01.atlas.de...
> Jack schrieb:
>> int add (const int &);
>> This function can't change the parameter value.
> Right.
> add() gets a reference to a constant int (read backwards).
>> But able to change its global value. Right?
> What does that mean?
> Like this ?
> # file.cpp
> int foo = 3;
> int add(const int& n)
> {
> n = 3; // compile error
> foo = 3; // OK
> return n;
> }
> //...
> int x = add(foo);
>
> /S
> --
> Stefan Naewe
> naewe.s_AT_atlas_DOT_de
Great. Thanks a lot
Jack
#2; Mon, 02 Jun 2008 10:31:00 GMT