The culprit

Hi
What'ws the error culprit of:

1
2
3
4
5
6
7
void test(int const & x)
{
     x=4;
}
main(){
       test(4);
}


2hat does a const& mean in the fist place?
The const keyword means that you can't change the value of x. So trying to do x=4; is illegal.

The & symbol means that x is a reference, rather than being passed in by value. This means that if the function changes the value of x, then the value of the variable in the calling code will also be changed. However, you're not passing in a variable - you're passing in an integer literal, so even if you weren't declaring x to be const, you wouldn't be able to do it anyway. After all, how could you change the value of 4?

What is it that you want your function to actually do?
Last edited on
void test(int x)
{
x=4;
}
main(){
test(4);
}


Your example above : In your case you should attach a variable, because "int const & x"
is an alias, not a regular value.

Edit : Usage :
1
2
3
4
main(){
int temp = 4;
       test(temp); //The second solution is allowed
}
Last edited on
2hat does a const& mean in the fist place?
The const doesn't belong to &. it belongs to int and tells the compiler that x is a refererence to const int
hence you cannot modify x.

you can write it as well like so: void test(const int & x)



@Jackson Marie

it's not just an alias. for const references the compiler generates a temoprary variable if the passed value can be implicitly converted to the type.
@coder777

But int * const cpc would declare a constant point to an int.Why would int& const x generate an error?

EDIT:it is interesting that 'const int ' ps the same as 'int const'
Last edited on
But int * const cpc would declare a constant point to an int.Why would int& const x henerate an error?


Look at
int& const
& followed by 'const' is 100 % illegal.
Last edited on
I sked why would it be illigal?
But int * const cpc would declare a constant point to an int.Why would int& const x generate an error?


Constness in pointer types is slightly more fiddly, because you can specify both that the pointer itself remains constant (i.e. the pointer will always point to the same memory address), and/or that the object pointed to will remain constant when accessed via the pointer.

It depends on where you put the word const. If you put it before the type of object pointed to, e.g.
const int* i;

then it means the value of the integer is constant. It is a pointer to a constant integer.

If you put it after the *, e.g.

int* const i;

then it means the pointer itself is constant. It is a constant pointer to an integer.

So, you can have the following:

1
2
3
4
5
6
int myValue = 0;
int const* i = &myValue;  //  This specifies that the pointer itself is constant

*i = 173;   // This is legal - the value of the integer being pointed to can be changed
int myOtherValue = 1;
i = &myOtherValue;   // ERROR - you cannot change the address that the pointer points to. 


Or you can have:

1
2
3
4
5
6
int myValue = 0;
const int* i = &myValue;  //  This specifies that the object pointed to is constant

*i = 173;   // ERROR - this is illegal - you cannot change the value of the integer being pointed to
int myOtherValue = 1;
i = &myOtherValue;   // This is OK - you can change the address of the pointer to point to another integer. 


You can even have:

1
2
3
4
5
6
int myValue = 0;
const int* const i = &myValue;  //  This specifies that the object pointed to is constant AND that the pointer itself is constant.

*i = 173;   // ERROR - this is illegal - you cannot change the value of the integer being pointed to
int myOtherValue = 1;
i = &myOtherValue;   // ERROR - you cannot change the address that the pointer points to. 


In your case, you are declaring that the pointer is constant, but not the integer itself. The compiler doesn't complain, because you are not changing the pointer, just the value of the integer.
A reference (&) can be set only once: at initialization. more const isn't possible hence adding const to the reference is considered illegal. It's a matter of definition
Topic archived. No new replies allowed.