32

I saw someone using this in one answer:

void methodA(const int*& var);

I couldn't understand what the argument means.

AFAIK:

  • const int var => const int value which can't be changed

  • const int* var => pointer to const int, ie *var can't be changed but var can be changed

  • const int& var => reference to const int, ie value of var can't be changed

What does const int*& var mean? Is const int& *var also possible?

Can you please give some example as well, like what can and can't be done with it?

UPDATE:

I am not sure if I am thinking the right way, but I began to think of a reference as an alias of the variable that was passed as argument, so:

const int * p;

methodA(p) => here we are passing p as const int * but we don't know if this is pass by value or what, until we see the definition of methodA, so if methodA is like this:

methodA(const int * & p2) ==> here p2 is another name to p, ie p and p2 are the same from now on

methodA(const int* p2) ==> here p2 is passed as value, ie p2 is just local to this method

Please correct me if I am thinking the wrong way. If yes, I might need to study some more about this. Can you please point to some nice references?

UPDATE 2:

If some beginner like me wants to know more about this thing, you can use the c++decl / cdecl program from here, which I just discovered to be very useful.

$ c++decl
Type `help' or `?' for help
c++decl> explain const int&* p
declare p as pointer to reference to const int
c++decl> explain const int*& p
declare p as reference to pointer to const int

But, as every one here pointed out, the first example isn't legal in C++.

5 Answers 5

31

It is a reference to a pointer to an int that is const.

There is another post somewhat related, actually, here. My answer gives a sorta of general algorithm to figuring these things out.

This: const int& *var has no meaning, because you cannot have a pointer to reference.

If the const's and pointers are getting in the way, remember you can typedef these things:

typedef int* IntPointer;
typedef const IntPointer ConstIntPointer;

void foo(ConstIntPointer&); // pass by reference
void bar(const ConstIntPointer&); // pass by const reference
void baz(ConstIntPointer); // pass by value

Might make it easier to read.


If you need more help on C++, read this. More specifically, references.

References as variables do not take space:

int i; // takes sizeof(int)
int*pi = &i; // takes sizeof(int*)

int& ri = i; // takes no space.
             // any operations done to ri
             // are simply done to i

References as parameters use pointers to achieve the end effect:

void foo(int& i)
{
    i = 12;
}

void foo_transformed(int *i)
{
    *i = 12;
}

int main()
{
    int i;

    foo(i); // same as:
    foo_transformed(&i); // to the compiler (only sort of)
}

So it's actually passing the address of i on the stack, so takes sizeof(int*) space on the stack. But don't start thinking about references as pointers. They are not the same.

8
  • in third method baz, ConstIntPointer == const int * var, how is this pass by value, there is pointer involved ?? I am confused now. Aug 10, 2009 at 22:20
  • Pointers take space themselves. Pointers are variables that store the address of another variable. It's going to copy that value, push it on the stack, and call that function.
    – GManNickG
    Aug 10, 2009 at 22:22
  • You are passing the value that is stored in the pointer (i.e. the memory address). Aug 10, 2009 at 22:22
  • The 3rd one is passing the pointer by value, whereas the previous two is passing the pointer by reference. We are talking about the passing the pointer here, not concerning ourself with that the pointer is pointing to.
    – KTC
    Aug 10, 2009 at 22:23
  • In the C++ spec., it is unspecified whether references take space.
    – geometrian
    Jan 9, 2013 at 17:53
10

Some folks find it easier reading this from right to left. So

const int*&

is a reference to a pointer to an integer that is const.

As you know, references cannot be changed, only what they refer to can be changed. So the reference will refer to just one pointer to an integer that is const. Since the pointer is not const - the integer is const - you can change the pointer to point to a different integer.

Compare this to

int* const &

This is a reference to a constant pointer to an integer. Again the reference is immutable, and in this case it is a reference to a constant pointer. What you can change in this case is the integer value since there was no const either side of the int keyword.

Just to add confusion, const int and int const are the same. However int const * and int * const are very different. The first is a pointer to a constant integer, so the pointer is mutable. The second is a constant pointer to an integer, so the integer is mutable.

Hope this helps!

3

In your example, var is a refernce to a pointer to const char.

Since it's a reference, a change to the parameter inside methodA() will be reflected in the argument that is passed to methodA():

void methodA( const char*& var)
{
    static const char newdata[] = {'a', 'b', 'c', '\0'};

    printf( "var points to %s\n", var);

    var = newdata;
}


int main()
{
    const char * p = "123";

    printf( "p points to: %s\n", p);      // prints "p points to: 123"
    methodA( p);
    printf( "now p points to: %s\n", p);  // prints "now p points to: abc"
}
3

It is a reference to a const pointer, i.e. a pointer where you cannot modify the data pointed to. As the reference is used as an argument to a method the method is able to modify the pointer to let it point to something else (still something that cannot be modified).

With regards to your update:

so if methodA is like this methodA(const int * & p2) ==> here p2 is another name to p, i.e. p and p2 are same from now on and if methodA(const int* p2) ==> here p2 is passed as value i.e p2 is just local to this method

Yes, you are correct.

0

Here's another example, a getter that returns the address of a private data item. The item happens to be an int for simplicity. A large array of items would be a more practical case (a zero-copy getter).

#include <iostream>
using namespace std;

class X {
public:
  void getter(const int *&data) const
  {
      data = &val;
  }
private:
  int val = 5;
};

main()
{
    X obj;
    const int *data;

    obj.getter(data);
    cout << data << endl;
    cout << *data << endl;
}
1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Sep 21, 2021 at 17:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.