Click here to Skip to main content
15,900,110 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Question about const int& Pin
Stephane Rodriguez.9-Feb-03 0:13
Stephane Rodriguez.9-Feb-03 0:13 
GeneralRe: Question about const int& Pin
George29-Feb-03 0:33
George29-Feb-03 0:33 
GeneralRe: Question about const int& Pin
Stephane Rodriguez.9-Feb-03 0:47
Stephane Rodriguez.9-Feb-03 0:47 
GeneralRe: Question about const int& Pin
George29-Feb-03 0:58
George29-Feb-03 0:58 
GeneralRe: Question about const int& Pin
includeh109-Feb-03 0:38
includeh109-Feb-03 0:38 
GeneralRe: Question about const int& Pin
George29-Feb-03 0:41
George29-Feb-03 0:41 
GeneralRe: Question about const int& Pin
Gary R. Wheeler9-Feb-03 5:06
Gary R. Wheeler9-Feb-03 5:06 
GeneralRe: Question about const int& Pin
Jambolo9-Feb-03 22:39
Jambolo9-Feb-03 22:39 
Ahhh, the beauty of const & parameters! I use these two conventions:

1. If a parameter is passed as a reference, it must always be a const reference.

In the following code, does foo change the value of bar or not?
foo( bar );

Most people would say no, but foo could be defined like this:
void foo( int & x ) { ++x; }

Since people are suprised when something like that happens, it is a source of bugs, so it is a bad idea. Don't change the value of parameters passed by reference. And since the reference is never going to change, you might as well make it const.

If the code looks like this...
foo( &bar );

...then people are not be surpised if the value of bar is changed by foo. So, if the value is changed, use *, If it isn't, use const &.

2. Prefer const & to const *.

So, why use const & when you can use const *? There are other reasons, but here is mine: A function call that doesn't change the value of a parameter should look like it doesn't change the value and vice versa.
void foo( int const & x ) { ... } // Neither of these ...
void foo( int const * x ) { ... } // ... functions change x (or *x)
...
foo( bar );    // This call does not appear to change bar
foo( &bar );   // This call appears that it could change bar
               // but it doesn't

GeneralESC closing dialog Pin
S O S8-Feb-03 23:18
S O S8-Feb-03 23:18 
GeneralRe: ESC closing dialog Pin
skaanji9-Feb-03 0:47
skaanji9-Feb-03 0:47 
GeneralRe: ESC closing dialog Pin
S O S9-Feb-03 3:22
S O S9-Feb-03 3:22 
GeneralDirectX HELP!!! Pin
Ehsan Baghaki8-Feb-03 22:15
Ehsan Baghaki8-Feb-03 22:15 
GeneralRe: DirectX HELP!!! Pin
Anonymous8-Feb-03 23:03
Anonymous8-Feb-03 23:03 
QuestionWhat means cout.precision (4)? Pin
George28-Feb-03 22:09
George28-Feb-03 22:09 
AnswerRe: What means cout.precision (4)? Pin
Mike Nordell9-Feb-03 2:07
Mike Nordell9-Feb-03 2:07 
QuestionHow to replace the system clock? Pin
dennisV8-Feb-03 21:06
dennisV8-Feb-03 21:06 
AnswerA soldering iron and quite some luck! Pin
Mike Nordell9-Feb-03 15:08
Mike Nordell9-Feb-03 15:08 
AnswerRe: How to replace the system clock? Pin
ELY M.21-Aug-04 5:13
ELY M.21-Aug-04 5:13 
GeneralRe: How to replace the system clock? Pin
dennisV21-Aug-04 14:15
dennisV21-Aug-04 14:15 
Generala global mouse hook Pin
king_of_the_world8-Feb-03 17:30
king_of_the_world8-Feb-03 17:30 
GeneralRe: a global mouse hook Pin
Paul M Watt8-Feb-03 20:13
mentorPaul M Watt8-Feb-03 20:13 
GeneralRe: a global mouse hook Pin
king_of_the_world8-Feb-03 20:26
king_of_the_world8-Feb-03 20:26 
GeneralRecursive CFtpFileFind Pin
User 127828-Feb-03 16:47
User 127828-Feb-03 16:47 
Generaltrying to shut down windows Pin
king_of_the_world8-Feb-03 15:53
king_of_the_world8-Feb-03 15:53 
GeneralRe: trying to shut down windows Pin
Abbas_Riazi8-Feb-03 17:39
professionalAbbas_Riazi8-Feb-03 17:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.