Thread: Pass by constant reference

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    52

    Pass by constant reference

    Pass by constant reference ensures that the argument won’t be modified BY THE FUNCTION. So if in the function you push the argument onto a vector then return from the function, you then have the argument stored in the vector and you can do whatever you want with it, right?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, but you will be doing whatever you want with a copy of the object. Using a vector for this is quite unnecessary.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    52
    Okay so in this example, where I'm simulating STL vectors with
    limit = a pointer to the memory location AFTER the last available one
    avail = a pointer to the next available memory location
    Code:
    template<typename T>
    void Vec<T>::push_back(const T & val)
    {
    if (avail == limit)
    grow();
    *avail = val;
    ++avail;
    }
    would the location pointed to by avail store the argument or a copy of it?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You would store a copy of it since you dereference the pointer and you assign the object itself and not its address.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    52
    Right... thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Knowing when function needs pass by reference.
    By bsdunx in forum C Programming
    Replies: 4
    Last Post: 08-29-2007, 12:38 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. Replies: 9
    Last Post: 01-29-2006, 06:57 PM