By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,125 Members | 1,257 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,125 IT Pros & Developers. It's quick & easy.

const type& and type const&

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.