Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Const Reference vs Value (C++)


  • Please log in to reply
2 replies to this topic

#1 MadDawg

MadDawg

  • Members
  • 453 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Houston, TX
  • Local time:03:55 AM

Posted 29 November 2011 - 11:34 PM

Alright, I've been looking around (see bottom of post) and I've read that pass by const reference is generally better than passing by value, unless you're dealing with the built-in types. Is this true (or rather, how is this true)?
For built-in types, do I get any benefit whatsoever in choosing one over the other?

I've also read that different compilers handle things, well, differently. If for whatever reason I wanted to use every C++ compiler available, what would be the best habit to pick up?

Here's some sample code I threw together while I was studying for a Calculus test (you can school me on the class definition if you please :P):

Pass by value:

Spoiler


Spoiler


Pass by const reference:

Spoiler


Spoiler


Here's where I've been:
http://stackoverflow.com/questions/270408/is-it-better-in-c-to-pass-by-value-or-pass-by-constant-reference
http://bytes.com/topic/c/answers/524505-const-reference-vs-value
http://forums.devx.com/archive/index.php/t-93763.html
http://www.cprogramming.com/tutorial/const_correctness.html

The reason I asked this is that I wanted to see some input from others, as well as having a somewhat centralized area to get this info.
A penguin broke my windows with a half-eaten apple!

BC AdBot (Login to Remove)

 


#2 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:02:55 AM

Posted 30 November 2011 - 07:17 PM

Other than for class, I have never once stopped to consider whether passing by reference is better than passing by value. It usually always works out that primitives are passed by value, and other data types are passed by reference. I can't think of any design meeting where anyone worried about how a parameter was handed around. As far as benefits, one thing I did not see mentioned was that unless you are working on a system with limited resources (like an embedded controller), or manipulating incredibly large amounts of data, there really will not be any noticeable benefit.

Regarding differences in compilers, the difference is in the compiler, not how you decide to program. An if statement is an if statement. A for loop is a for loop. You don't write a loop one way for one compiler, and a different way for a different compiler. Again, the caveat to this would be if you are using a specific compiler for a specific task (ie, an embedded system with limited resources).

#3 MadDawg

MadDawg
  • Topic Starter

  • Members
  • 453 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Houston, TX
  • Local time:03:55 AM

Posted 03 December 2011 - 03:47 AM

Thanks for the input. I understand and agree with your points. That said, I have a habit of trying to code in a way that would allow my applications to run on dated/limited computers. I just love efficiency. :)
A penguin broke my windows with a half-eaten apple!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users