Noob question about & and const

Jul 29, 2014 at 9:27pm
Hi, could someone be kind enough to explain me this piece of code?

 
  const int*const Method3(const int*const&)const;



Last edited on Jul 29, 2014 at 9:47pm
Jul 29, 2014 at 9:31pm
const means the variable will never change.
int aka signed int is an integer, 4 bytes, the limits are –2,147,483,648 to 2,147,483,647.
Jul 29, 2014 at 9:32pm
* is a pointer, a pointer points to another variable's address.
Jul 29, 2014 at 9:33pm
Method3 is the name of the variable.
Jul 29, 2014 at 9:43pm
i don't think it's a varible, more like method declaration
Last edited on Jul 29, 2014 at 9:47pm
Jul 29, 2014 at 10:10pm
Good point.
Jul 29, 2014 at 10:13pm
In this case the return type is a constant pointer to a constant integer and the parameter is a reference to a constant pointer to a constant integer. The const after the function means it is a member function(method) of a class and it promises not to modify any of the member variables.

By the way @Bob
Bob The Zealot wrote:
int aka signed int is an integer, 4 bytes, the limits are –2,147,483,648 to 2,147,483,647.
This is not always the case it is dependent on the compiler. http://www.cplusplus.com/doc/tutorial/variables/

Oh and something I forgot to mention earlier is that the keyword const binds to the left unless it is the first keyword then it binds to the right. so const int is the same as int const
Last edited on Jul 29, 2014 at 10:36pm
Jul 29, 2014 at 10:38pm
The word const refers to the identifier to its left, the exception to the rule is that the first identifier's const can be on the left:

1
2
3
4
5
6
7
const int;         // constant int
int const;         // constant int
const int *;       // pointer to constant int
int const *;       // pointer to constant int
const int * const; // constant pointer to constant int
int const * const; // constant pointer to constant int
int * const;       // constant pointer to non-constant int 


Last edited on Jul 29, 2014 at 10:41pm
Jul 30, 2014 at 1:43pm
Thanks it helps alot, I get it now!
Jul 30, 2014 at 5:39pm
Your welcome Geranimo, thanks to LowestOne and giblit.
Topic archived. No new replies allowed.