parameter passing question

This is a discussion on parameter passing question within the C++ Programming forums, part of the General Programming Boards category; I'm new to C++ and have looked around for the answer to this seemingly simple question but can't seem to ...

  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
    20,961
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

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

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