469,160 Members | 1,832 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,160 developers. It's quick & easy.

const type& and type const&

What is practic difference between this two declarations?
If i want call my func with func("blah") i could write:
1) func(std::string const& arg1)
2) func(const std::string& arg1)
Whats better to use if i dont want to change content of arg1 it in func
body?
--
<< pozdrawiam -lysek- @ irc.freenode.net#linux.com.pl
<< prompt$ :(){ :|:& };:
<< echo mail | sed 's/__NOSPAM//g'

Jul 23 '05 #1
6 13967
p|OtrEk wrote:
What is practic difference between this two declarations?
If i want call my func with func("blah") i could write:
1) func(std::string const& arg1)
2) func(const std::string& arg1)
Whats better to use if i dont want to change content of arg1 it in func
body?


They're the same.

The types are read from right to left.

consider:

const int i; // i is a const int
int const i; // i is a const int
const int * i // i is a pointer to a const int.
int const * i // i is a pointer to a const int
int * const i // i is a const pointer to an int
const int * const i // i is a const pointer to a const int
int const * const i // i is a const pointer to a const int

References cannot be reseated (c.f., pointers), so they are already "const".

It's usually easier to read:
const int i;

yet, when stacking them up, it's usually easier the other way around:
int const * const

Ben
--
I'm not just a number. To many, I'm known as a String...
Jul 23 '05 #2
Ben Pope wrote:
They're the same.
(...)
References cannot be reseated (c.f., pointers), so they are already

"const"
Thank you.
--
<< pozdrawiam -lysek- @ irc.freenode.net#linux.com.pl
<< prompt$ :(){ :|:& };:
Jul 23 '05 #3
On more thing,
is this legal to write:
const int& fun () { return 1; } // so it returns reference to const '1'
??
--
<< pozdrawiam -lysek- @ irc.freenode.net#linux.com.pl
<< prompt$ :(){ :|:& };:
Jul 23 '05 #4
lysek wrote:
On more thing,
is this legal to write:
const int& fun () { return 1; } // so it returns reference to const '1'


You'd be returning a reference to a local... it would have lost it's scope by the time the function returned. This is undefined behavious, as far as I know.

It's usually best/safest to return by value.

Ben
--
I'm not just a number. To many, I'm known as a String...
Jul 23 '05 #5
Ben Pope wrote:
lysek wrote:
On more thing, is this legal to write:
const int& fun () { return 1; } // so it returns reference to const '1'


You'd be returning a reference to a local... it would have lost it's scope by the time the function returned. This is undefined behavious, as far as I know.


1 is an integer literal, not a local variable.

Jul 23 '05 #6
Mercator wrote:
Ben Pope wrote:
lysek wrote:
On more thing, is this legal to write:
const int& fun () { return 1; } // so it returns reference to const '1'


You'd be returning a reference to a local... it would have lost it's scope by the time the function returned. This is undefined behavious, as far as I know.

1 is an integer literal, not a local variable.


Yeah, I guessed somebody would mention that when I hit send. I was kind of extrapolating his use from the previous example, I guess this is why posting ACTUAL code rather than
noddy examples that are supposed to be equivalent, but aren't, is never a good idea.

Ben
--
I'm not just a number. To many, I'm known as a String...
Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by zealotcat | last post: by
reply views Thread by tom olson | last post: by
1 post views Thread by Kip | last post: by
8 posts views Thread by vsgdp | last post: by
9 posts views Thread by miaohua1982 | last post: by
13 posts views Thread by dragoncoder | last post: by
2 posts views Thread by ek | last post: by
2 posts views Thread by nassim.bouayad.agha | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.