I've just seen a function like show below, can someone explain why the const is after the int*?
Code:void swap( int* const ptr1, int* const ptr2 )
{
int temp;
temp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = *temp;
}
Printable View
I've just seen a function like show below, can someone explain why the const is after the int*?
Code:void swap( int* const ptr1, int* const ptr2 )
{
int temp;
temp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = *temp;
}
It tells you that ptr1 and ptr2 are constants (within this function at least)
So it stops you from doing (by mistake) something like
ptr1 = &temp;
If you try it, the compiler will complain about it.
So its the same as:
void swap( const int* ptr1, const int* ptr2 )
I've just never seen that format anywhere before.
With thanks to Scott Meyers (Effective C++) :
Meyers continues his explanation by suggesting that an imaginary vertical line be drawn through the asterisk and ifCode:char *p = "Hello"; // non-const pointer,
// non-const data
// note: deprecated from C++ Standard
// const char [] is standard for string literal types
const char *p = "Hello"; // non-const pointer,
// const data
char * const p = "Hello"; // const pointer,
// non-const data
const char * const p = "Hello"; // const pointer,
// const data
a. 'const' is to the left of the line, what's pointed to is const;
b. 'const' is to the right of the line, the pointer is const, and;
c. 'const' is on both sides of the line, both on const.
No doubt more than you were interested in asking about, but pointers - at least for those with minimal, or no, background in C - do seem troublesome. I also found the "vertical line" imagery helpful with regard to 'const'. :)
-Skipper
> So its the same as:
> void swap( const int* ptr1, const int* ptr2 )
No, this is the exact opposite
ptr1 = &temp; // is valid
*ptr1 = temp; // is invalid
See skipper's post
Thanks Skipper, Salem - thats exactly what I was looking for. I can sleep at night again now :)
A good way to keep this straight in your head is to read the statement backwards...
int const x; // x is a constant int
int const *x; // x is a pointer to a constant int
int *const x; // x is a constant pointer to an int
int const *const x; // x is a constant pointer to a constant int
In the first statement - once initialized - x cannot be reassigned a new value.
In the second statement x can be reassigned to a new memory address, but only an address containing a const int type.
In the third statement - once initialized - x cannot be reassigned to a new memory address, but the int type value at that address can be reassigned a new value.
In the fourth statement both the address and the value at that address cannot be reassigned after initialization, they are both constant
DarkStar,
Good information and a nice twist on what's been presented. :)
-Skipper
Why does this compile without problems though?
CString const* getSQL( );
What effect would this have?
> CString const* getSQL( );
The function returns a pointer to a const CString
It basically means you wont be able to modify the CString via this pointer (you can look, but you can't touch)
same asthen?Code:const CString* getSQL( )