& symbol and const datatype

hello eveyone.
1) i am working in stack program and i know that if i say
that means the address of y was assigned to x.
but i am not understand is when using it like:
const int & top() const;
2) here what the issue of saying const in the right an left

Last edited on
const int & top() const;
Is the declaration of a function top() returning a reference to a int which cannot be changed through the reference. The function itself is const, meaning it doesn't change any object/class variables (except they are declared mutable).

const int* x (or const int& x) is a pointer/reference to a const int, i.e. the value pointed to cannot be changed. int* const x means that the pointer value (i.e. the address pointed to) cannot be changed, but the value pointed to can be altered. Example:
const int* x = (const int*)0x100000;
*x = 5; // error: value cannot be altered
x = (const int*)0x200000; // OK
int* const y = (int*)0x100000;
*y = 5; // OK
y = (int*)0x200000; // Error: pointer value is const 
Topic archived. No new replies allowed.