Thread: const reference formal arguments

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    153

    const reference formal arguments

    Hey there everyone,
    I have another brain teaser for ya...this was just something I read that didn't quite make sense to me, maybe someone has a more insightful understanding of what I'm about to bring up. So currently I'm learning about passing function arguments by reference. In the middle of learning this I stumbled upon a passage in one of my C++ books that said (and this is a direct quote): "if your intent is that a function use the information passed to it without modifying the information, and if you're using a reference, you should use a constant reference..."
    Now correct if I'm wrong but isn't the point of passing by reference to make it so you're working with the actual data from the calling function and thus you're making it a point to alter the data? Why would you create a const reference? I was just wondering if this is bad information or if there is actually a necessity for a const reference somewhere along the line, as far as my understanding goes right now (which, I know, isn't too far...but it's far enough to make me question what I just read) a const reference seems unnecessary...any further revelations into this curiosity of mine would be really cool...thanks to anyone who replies to this -Chap

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    113
    well what i understant and actually i allways use const references because:

    if you pass by value you are wasting memory

    so i always pass by reference, but here is a problem
    when i want to pass an argument that can or must be modified in the function theres no problem but what happens if i dont want that this argument be modified ? and i still want the performance of passing by reference? i use const reference, passing the memory address to the function(good speed) and the function cant modify the argument.

    hope it helps and please excuse my poor english

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    One of the primary benfits of pass-by-reference is that you only pass the address of the object, instead of making a copy of that object's data.

    For most types that are built-in (like int, char and double), it doesn't matter, because there isn't much data to copy. But for user defined types (like string, vector, or most classes that you write), copying the data in the object is unnecessary and inefficient. Therefore, you pass by reference instead of by value.

    If you are using pass-by-reference, but don't plan on modifying the object, then you indicate that by using const. Also, using a reference instead of a pointer tends to be more intuitive (you can pass-by-reference using pointers also). So generally you should use a const reference when passing non-POD objects to functions unless you have some other reason not to do so.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    oh ok...I was looking at it from the perspective of fundamental data types...that makes more sense, I havent delved into classes yet so when I get there it'll provide more insight in regards to const references...oh and thanks for the info on how passing by value wastes memory...nice to know...thanks guys.-Chap

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>the info on how passing by value wastes memory
    Although this is true in some cases, in many cases the greater problem is copying the data. If you have pointers and stuff in a class or structure, you could have problems with copying the structure unless you write a foolproof copy constructor and/or assignment operator (you'll learn about them later). Add to that what jlou said about the time spent copying a huge amount of data stored in a structure for each function call, and you have a pretty solid case for using const references
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Quote Originally Posted by Hunter2
    If you have pointers and stuff in a class or structure, you could have problems with copying the structure unless you write a foolproof copy constructor and/or assignment operator (you'll learn about them later).
    Every time you write a class you should make sure that it has a valid copy constructor and assignment operator at all times. This could mean using the default compiler-generated versions, or it could mean declaring them as private and not implementing them because you don't want your class to be copied, or it could just mean implementing them correctly from the beginning. Whatever the solution, you should get in the habit of thinking about it every time.

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Yeah, you should always have a definite plan for the copy ctor, operator=, but as jlou pointed out, copy semantics may not make sense for your object, so you declare them private. If this is the case, you can't pass by value and must pass by reference or pass a pointer to the object (and the reference is generally nicer to deal with if possible).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Good points, I'll keep them in mind. *sigh* Time to stop being lazy...
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Well, it is certainly acceptable to use the default copy semantics, as long as they make sense. Just make sure that they do.

    As a matter of personal preference, I usually write out all my copy ctors and assignment operators, even if they only perform the equivalent of the default operation.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using Vectors. MinGW warning
    By Viewer in forum C++ Programming
    Replies: 9
    Last Post: 03-26-2009, 03:15 PM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Replies: 4
    Last Post: 01-23-2008, 06:21 AM
  4. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM