• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 159
  • Last Modified:

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.
0
Unimatrix_001
Asked:
Unimatrix_001
  • 3
  • 3
3 Solutions
 
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now