| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"dolphin" |
| Date: |
23 Aug 2007 10:12:42 PM |
| Object: |
Why should we use fun(const Class &B) instead of fun(Class &B)? |
Why should we use fun(const Class &B) instead of fun(Class &B)?
.
|
|
| User: "Jim Langston" |
|
| Title: Re: Why should we use fun(const Class &B) instead of fun(Class &B)? |
24 Aug 2007 12:24:09 AM |
|
|
"dolphin" <jdxyw2004@gmail.com> wrote in message
news:1187925162.776741.174360@e9g2000prf.googlegroups.com...
Why should we use fun(const Class &B) instead of fun(Class &B)?
fun( const Class& B ) can accept a constant, or non constant instant of
Class, wereas fun( Class &B ) can only accept a non constant.
Consider a library I use where a certain function is declared similar to:
output( int x, int y, char * c )
Seems reasonable, right? Although output does not change the pointer
pointed to in the variable c. And since I normally use std::strings to do
output, I would like to do:
output( 10, 20, MyString.c_str() );
however, that fails. Why? Because c_str() returns a const char *, and that
can not be accepted by a function taking a non constant. So I have to
const_cast it, a real pain:
output( 10, 20,const_cast<char*>( MyString.c_str() ) );
Now consider your fun. Can you think of any possible time someone may want
to pass it a const Class? But they won't be able to without const casting
it, but then since it's not declared const, they won't be sure if fun is
modifying the instant of Class.
const correctness is a good thing. If you're not going to change a
parameter, it should be declared const.
.
|
|
|
|
| User: "Alf P. Steinbach" |
|
| Title: Re: Why should we use fun(const Class &B) instead of fun(Class &B)? |
23 Aug 2007 10:18:08 PM |
|
|
* dolphin:
Why should we use fun(const Class &B) instead of fun(Class &B)?
Which to choose depends on the context.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
.
|
|
|
|
| User: "Neelesh Bodas" |
|
| Title: Re: Why should we use fun(const Class &B) instead of fun(Class &B)? |
24 Aug 2007 01:17:58 AM |
|
|
On Aug 24, 8:12 am, dolphin <jdxyw2...@gmail.com> wrote:
Why should we use fun(const Class &B) instead of fun(Class &B)?
1. gives the guarantee that B will not be changed (because you can
only call const member functions)
2. can be called on non-modifiable lvalues (Eg const objects,
temporaries, and for POD's - literals.)
-N
.
|
|
|
| User: "Jorgen Grahn" |
|
| Title: Re: Why should we use fun(const Class &B) instead of fun(Class &B)? |
25 Aug 2007 03:19:25 AM |
|
|
On Fri, 24 Aug 2007 06:17:58 -0000, Neelesh Bodas <neelesh.bodas@gmail.com> wrote:
On Aug 24, 8:12 am, dolphin <jdxyw2...@gmail.com> wrote:
Why should we use fun(const Class &B) instead of fun(Class &B)?
1. gives the guarantee that B will not be changed (because you can
only call const member functions)
Nitpick: the object that B refers to will not be changed through B.
It can be changed through other, non-const, references and pointers.
2. can be called on non-modifiable lvalues (Eg const objects,
temporaries, and for POD's - literals.)
3. Documentation.
Because fun(Class& b) only offers you one thing which fun(const Class& b)
doesn't -- passing results from fun() by modifying b -- readers will
assume that fun(Class& b) does indeed do that.
/Jorgen
--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyndns.org> R'lyeh wgah'nagl fhtagn!
.
|
|
|
|
|

|
Related Articles |
|
|