Thread: parameter passing question

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    6

    parameter passing question

    I'm new to C++ and have looked around for the answer to this seemingly simple question but can't seem to find it. Can someone please explain to me the difference between a function that looks like "funct( const type *& var )" and and one that looks like "funct( const type *var)" ?

    I know the first one is a pass by constant reference, but I'm not sure what const means in the second one. I thought you needed the "&" to make it a pass by reference, so without what does that make the second one? I appreciate your answers.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    One trick is to read the tokens from right to left. You see the &, so you know that a reference is involved. You see the *, so you know that it is a reference to a pointer. Then you see the const type, which can also be written as type const, and thus you know that it is a reference to a pointer to const type. Therefore, the object of the type cannot be changed through the pointer, but you can change the pointer, and a change to the pointer will be reflected in the caller due to it being a reference parameter.

    For the second one, it is the same thing, except that it is not a reference parameter, so if you assign to the pointer, only the local pointer is changed.

    If you actually wanted a const reference to a pointer to non-const, you would write it as funct( type* const& var ). Now, reading the tokens from right to left, you see that the const applies to the pointer.
    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
    Feb 2010
    Posts
    6
    thanks, that helped a lot. So does the const in the second one even do anything? if it's just passing the pointer by value (making the local variable a copy of the pointer being passed), you don't have access to the type (which is const) that the pointer is pointing to to change it in the first place, right?

    is the const just there as a formality, meaning that whatever pointer variable is passed into this function, it MUST be a pointer to a const type (even tho the pointer is just going to be copied)? and if that's the case, then does that mean the local variable (which is a pointer) can be changed to point to ONLY types that are const?

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Ace Rockolla View Post
    thanks, that helped a lot. So does the const in the second one even do anything? if it's just passing the pointer by value (making the local variable a copy of the pointer being passed), you don't have access to the type (which is const) that the pointer is pointing to to change it in the first place, right?
    wrong


    it does copy the pointer. but the memory it points to - is still the same as in the calling function

    const tell the compiler that the function does not intend to change the contents of the memory that is pointed by the pointer
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't fully understand what you are saying, so let's sum it up:
    const T* -> pointer to const T. Hence if you dereference the pointer, you get const T, which cannot be assigned, only read. Of course, you can also assign it a different address, which is then implicitly converted to const T*. So there is an idea behind taking a parameter to const - it makes sure the function cannot modify what it points to.
    Any T -> const T is implicit. That is, you can pass in any variable of type T to a function that expects a const T. However, the reverse is not true.
    T* const -> a constant pointer to T. Hence you cannot change the value of the actual pointer, but you can change what it points to.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing "this" as function parameter
    By pgavigan in forum C++ Programming
    Replies: 15
    Last Post: 07-13-2007, 10:06 AM
  2. Problem passing double array as a function parameter
    By pppbigppp in forum C++ Programming
    Replies: 7
    Last Post: 06-06-2006, 03:08 AM
  3. QUESTION!! parameter passing
    By jave in forum C Programming
    Replies: 8
    Last Post: 10-21-2005, 12:50 PM
  4. general question regarding a function parameter
    By mlupo in forum C Programming
    Replies: 7
    Last Post: 10-13-2002, 07:32 PM
  5. Question about Templates and passing arguments
    By supaben34 in forum C++ Programming
    Replies: 2
    Last Post: 10-13-2002, 01:32 AM