Why do I want a const modifier on pass-by-reference?

This is a discussion on Why do I want a const modifier on pass-by-reference? within the C++ Programming forums, part of the General Programming Boards category; int increment(const int& myvalue); I'm considering here the apparent uselessness (If I don't want my argument to be changed, why ...

  1. #1
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317

    Why do I want a const modifier on pass-by-reference?

    int increment(const int& myvalue);

    I'm considering here the apparent uselessness (If I don't want my argument to be changed, why did I declared a reference to it in the first place?)

    Since not everything is what it seems at first glance (certainly not in C/C++), I started to think of the possible reasons of why would I want to do such a thing.

    I came up with the following:
    Pass-by-reference avoids the waste of creating space in memory to accomodate a copy of the variable passed to a function (which is what happens on a pass-by-value). So, maybe the above can be useful if the argument to be referenced is a big one (memory-wise). This way I save memory while the function is running, since no copy of the variable was created. Just a reference to its memory location. Big arrays of strings , objects and multi-dimensional arrays are candidates here.

    My question is, is this right? Or am I overlooking yet some other advantage?
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  2. #2
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Yeah I think so, when you pass by value a copy constructor is created, but when you pass by reference this isn't the case, as far as I know.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Well you are right. If I have a huge class I need to pass to a function I don't want to create a copy so I send it by reference which you already know.

    /* Prototype #1 */
    bool Foo( MyClass & );

    Anyways, the const comes in because I want to make absolutely sure that my function does not tamper with MyClass. This is something that needs to be taken in account when you pass by reference, that if you actually change something in MyClass it changes it for good. So I use the const keyword to indicate I don't want to change the value, just get some values from the Class perhaps.

    /* Prototype #2 */
    bool Foo( const MyClass & );

    Hopefully that helped clear up something.

    --MrWizard
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  4. #4
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    Originally posted by MrWizard
    Hopefully that helped clear up something.
    Thanks both.

    Yes, it did. Problem with these books nowadays is that they explain what things are but fail to either explain why things are that way or give real life examples.

    The learning process is much easier if one can relate what he learns with true practice. Hence the learn by example method being one of the most successful.

    Anyway, don't mind my ranting... Thanks again.
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. Memory leak - need help finding
    By ChadJohnson in forum C++ Programming
    Replies: 8
    Last Post: 04-06-2005, 08:26 PM
  5. Problem with Template Function and overloaded equality operator
    By silk.odyssey in forum C++ Programming
    Replies: 7
    Last Post: 06-08-2004, 05:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21