Reasons for using references on a basic type.

Hi,

Is there any reason why the following would be used:
     void function(const bool &aValue);
over just:
     void function(const bool aValue);

Uni.
LVL 3
Unimatrix_001Asked:
Who is Participating?
 
Kent OlsenData Warehouse Architect / DBACommented:

Hi uni,

The two forms actually do radically different things.  :)

The second form puts a boolean value on the stack and passes it to the function.  The function the uses is as a boolean value.

The first form puts an address on the stack.  The variable aValue contains the address of the value passed to the function, not the actual value.  This technique is required if the function is going to change the value of aValue in the calling program or if the function is being passed an array of items.

You'll likely hear the terms "pass by reference", "pass by address", and/or "pass by value".  They are common terms in C programming and indicate whether a data value is placed on the stack or an address.



Good Luck,
Kent
0
 
Unimatrix_001Author Commented:
Hi kent,

Does passing by address make any positive impact on performance when working with a single variable of a basic type as above?

Thanks,
Uni
0
 
jkrCommented:
Well, usually you past value as a non-const reference if you want the called finction to be able to change that parameter and have the caller to be able to reflect that change. For POD types like 'bool', that is the only motivation I could think of to do so. If you waant to just save the overhead of passing a complex type by value, make it a const reference or a ('const const') pointer.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
jkrCommented:
Awfully dry to read, yet equally invaluable: http://en.wikipedia.org/wiki/Const-correctness
0
 
Unimatrix_001Author Commented:
Does passing by address make any positive impact on performance when working with a single variable of a basic type as above?
0
 
jkrCommented:
No, not at all - think of that is pointer dimensions, a 'bool' is an 'unsigned char', a 'bool*' is four bytes on 32 bit systems - the balance is reached when the refereced type  (class/struct) is larger than a pointer to it.
0
 
Unimatrix_001Author Commented:
Okay, I didn't think so, but thought it better to check.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.