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++.
Integer constant in C is a data type that is represented by const int . const int is capable of storing an integer in decimal, octal, and hexadecimal bases. The value to const int is assigned only when it is declared and cannot be changed afterward.
In the type int const , const is a type qualifier, and int is a type specifier. Here, const qualifies the type int . Qualifiers change the semantics of the type in some way. Other type qualifiers include volatile and restrict . (Note you can also write const int , which means the same thing as int const .
The const keyword specifies that a variable's value is constant and tells the compiler to prevent the programmer from modifying it. // constant_values1.cpp int main() { const int i = 5; i = 10; // C3892 i++; // C2105 }
I had it drilled into my head that const int *ptr=&i; means that the pointer stores the address of a constant,while int * const ptr=&i; means the pointer is itself constant and can't change.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With