This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What is the difference between const int*, const int * const, and int const *?
I am trying to understand how const works in a function parameter. What is the difference between void function(int const *&i); and void function(const int *&i);? And I would appreciate if you gave an example of their implementation and of the usage of i.
They are the same, just read it from right to left:
First example: i is a reference to a pointer of a constant integer.
Second example: i is a reference to a pointer of an integer constant.
The placement of const only changes things on either side of the pointer. For example:
const int* i
means: i is a pointer to a constant integer
whereas int* const i
means: i is a constant pointer to an integer.
Those declarations are equivalent. For more info, see this page.
Best place for const correctness is the - C++ FAQ Lite
They're both the same.
It means a reference to a pointer to a const int.
Related
This question already has answers here:
C++ Const Usage Explanation
(12 answers)
Closed 7 years ago.
I'm new to using C++ to write enterprise software at my company and the usage of const in member functions has been quite confusing to me. For example, I have the following method:
string DAC::impulse_find(int i)
There will be no modification of the variable i. My code compiles perfectly when I use these three variants of adding const to the above method:
const string DAC::impulse_find(const int i) const
const string const DAC::impulse_find(const int i) const
string const DAC::impulse_find(const int i) const
So what exactly is the difference between these three? I looked at some past answers and say "It applies to whatever is to the left" but that is still confusing. Does it apply to anything, such as the name of the class as well?
There is no difference between
const std::string
std::string const
The order of const is irrelevant since you are not dealing with pointers.
Having many const as a qualifier doesn't make sense. Did you turn up the warning level on your compiler to maximum?
Edit 1: Pointers and Const
Since you are returning a copy of a variable the effectiveness of const is worthless. The original item from the function can't be modified because you are returning a copy.
Maybe you are confused with the pointer syntax, where the placement of const matters:
string const * const -- constant pointer to a constant (read-only string). Pointer cannot be modified.
string const * -- Mutable pointer to a constant (read-only) string. Pointer value can change.
string * const -- Constant pointer to a read/write string. Pointer value cannot be changed.
string * -- Mutable pointer to a read/write string. Pointer value can be changed.
The const for read-only data may be placed before or after the type:
const string *
string const *
Both of the above are equivalent.
All three version
const string …
const string const …
string const …
are identical.
The placement of const doesn't matter here. The duplicate const in the second version is redundant and your compiler should give a warning about it.
Moreover, the consts don't make sense here because the string is returned as a copy. Returning a const value brings you nothing (see KABoissonneault's comment), only references or pointers to const make sense as a return type.
This question already has answers here:
What is the difference between const int*, const int * const, and int const *?
(23 answers)
Closed 8 years ago.
I was wondering what two constant signs on a function parameter does in this case?
void virtual_via_pointer( const Employee * const );
This isn't specific to function parameters.
const Employee*
Means a "mutable pointer to a constant instance of Employee".
Employee* const
Means a "constant pointer to a mutable instance of Employee".
const Employee* const
Means a "constant pointer to a constant instance of Employee".
See also the Spiral Rule.
The pointer and the pointee are both constant.
This question already has an answer here:
What is Node *& aNode? [duplicate]
(1 answer)
Closed 10 years ago.
I don't know much about C++ and I need to deal with a function at the moment. What does this mean in a function prototype?
void myFunc(int &size, signed char *&array);
This is a reference to a pointer. So you don't pass a copy of the pointer to the function, instead you pass a reference to it, which means any changes to that value in the function would actually affect the original pointer you passed as argument.
It means array is a reference to a signed char *, i.e. to pointer.
*& is a reference to a pointer
You suppose to call myFunc like this:
int size;
signed char *p;
myFunc(size, p);
Normally pass a reference to pointer to a function, so could change the pointer inside the function.
think of it in this way:
(int) &size;
(char*) &array;
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Declaring pointers; asterisk on the left or right of the space between the type and name?
I've always been wondering what's the exact correct position to put * and &. it seems that C++ is pretty tolerant about where to put these marks.
For example I've seem pointer and ampersand been put on both left and right of a keyword or in the middle of two keywords, but confusingly sometimes they seems to mean the same thing, especially when used together with const
void f1(structure_type const& parameter)
void f2(structure_type const ¶meter)
void f2(structure_type const *sptr);
void f2(structure_type const* sptr);
void f2(structure_type const * sptr);
The examples are not exhaustive. I see them everywhere while being declared or been passed to a function. Do they even mean the same thing? But I also see cases while putting * will affect which object being referred as a pointer (likely the case where * is in between two keywords).
EDITED:
int const *Constant
int const * Constant // this above two seem the same to me, both as pointer to a constant value
int const* Constant // EDIT: this one seems the same as above. instead of a constant pointer
const int * Constant // this is also a pointer to a constant value, but the word order changed while the pointer position stays the same, rather confusing.
int* const Constant
int * const Constant // instead, these two are constant pointers
So I concluded this:
T const* p; // pointer to const T
const T* p // seems same from above
T* const p; // const pointer to T
Still, this confused the hell out of me. Doesn't the compiler care about position and the spacing required for them?
Edit:
I want to know in general of the position matters. If yes in what cases.
White space matters only to the degree that it keeps tokens from running together and (for example) creating a single token, so (for example) int x is obviously different from intx.
When you're dealing with something like: int const*x;, whitespace on either size of the * makes absolutely no difference to the compiler at all.
The difference between a pointer to const int and const pointer to int depends on which side of the * the const is on.
int const *x; // pointer to const int
int *const x; // const pointer to int
The primary difference is readability when/if you define/declare multiple objects in the same declaration.
int* x, y;
int *x, y;
In the first, somebody might think that x and y are pointers to int -- but in fact, x is a pointer to an int, and y is an int. To some people's eyes, the second reflects that fact more accurately.
One way to prevent any misinterpretation is to only ever define one object at a time:
int *x;
int y;
For any of these, correct interpretation is fairly easy if you ignore whitespace entirely (except to tell you where one toke ends and another starts, so you know "const int" is two tokens) and read from right to left, reading * as "pointer to". For example: int volatile * const x; is read as "x is a const pointer to a volatile int".
int const *Constant
int const * Constant
int const* Constant
All of the above intend to declare a non constant pointer to a constant integer.
Simple rule:
If const follows after the * it applies to the pointer if not it applies to the pointed object. The spacing doesn't matter.
Both & and * placements in variable declaration are acceptable and only depends on your own flavour. They strictly mean the same thing, creating respectively a pointer and a reference.
However, the const keyword placement is primordial, as a int const* variable declares a constant pointer to a non-constant int, and const int* variable is a non-constant pointer to a constant int.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
const int = int const?
What is the difference between these combination of keywords ?
const int*
int const*
int* const
Is it possible to modify the value of a constant by accessing its address using a pointer ?
const int*
int const*
Both declare an pointer to an constant type int. It means that the pointer can be made to point to(hold) any other address but any attempt to modify the constant integer data type will result in Undefined Behavior.
int* const
Declares an constant pointer to an int type. It means that the pointer cannot be assigned any new address it will always keep pointing to the same address, but the integer it points to can be changed.
Is it possible to modify the value of a constant by accessing its address using a pointer ?
Yes, it is possible but it leads to a ill formed program exhibiting an Undefined Behavior.
In general:
const T === T const
and
const T * === T const *
but
T * const
makes the pointer constant, but not necessarily the contents of T.
const T * const === T const * const
will make both the contents of T and the pointer itself constants.
As for changing the contents of a const: you shouldn't be able to, but the compiler/OS/CPU don't try too hard to stop you, so you get what is colloquially called “undefined behaviour,” which is a nice way of saying “random crashes on some systems”
const int* and int const* are both a pointer to a constant int. This means that the value pointed to can not be changed but the pointer itself can be.
The third example int *const is a constant pointer to an int. This means that the value can be changed but the pointer can not.
Yes you can change the value of the address by using a pointer but doing so is undefined behavior and should be avoided. C++ also offers a const_cast to remove the constness of a variable.
If you have any other questions related to the const keyword you should reference the const correctness section of the C++ FAQ.
The first two are a variable pointer to a const int. The pointer itself can be changed. Without casting away const, the int can't be changed through this pointer. (It may, however, be modifiable through non-const references.)
The last is a const pointer to a variable int. Without casting away const, the pointer itself can't be changed. The int can be changed through the pointer.
This may be helpful. This is another one that talks about breaking it apart so you can see which parts become const in various scenarios.
Const binds to the left.
Unless it is the left most clause then in binds right.
Just read right to left
const int* => (const int)* => "pointer" to "const int"
=> Pointer can change.
=> Can't change the value it points at (the int is const)
int const* => (int const)* => "pointer" to "const int"
=> As above
int* const => int (* const) => "const pointer" to "int"
=> Can't change the pointer (its const) it always points
=> at the same things.
=> You can change the value of the object it points at.