简体   繁体   中英

What does this mean const int*& var?

I saw someone using this

void methodA(const int*& var);

in one answer, but couldn't understand what the argument means.

AFAIK:

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

  • const int* var => pointer to int which is const 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, and is const int& *var also possible?

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

UPDATE :

I am not sure if I am thinking right way, but I began to think reference as alias of the variable that was pass as argument, so const int * p; methodA(p) => here we are passing p as const int * but we dont know if this is pass by value or what until we see the definition of method A,

so if methodA is like this methodA(const int * & p2) ==> here p2 is another name to p, ie p and p2 are same from now on and if 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 wrong way ? If yes, I might need to study some more about this ? Can you please point some nice references ?

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

$ 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, first example isnt legal in C++.

Thanks

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.

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!

It is a reference to a const pointer, ie 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, ie p and p2 are same from now on and if methodA(const int* p2) ==> here p2 is passed as value ie p2 is just local to this method

Yes, you are correct.

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"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM