问题
I am a beginner to C++ and have the following question regarding the purpose of pointers where neither the address nor the value that it is pointing to can be changed (constEverything
in the example).
Example:
int main()
{
int i = 1;
int j = 2;
int* const constPoint = &i;
*constPoint = 3; // legal
constPoint = &j; // illegal
const int* constVal = &i;
*constVal = 3; // illegal
constVal = &j; // legal
const int* const constEverything = &i;
*constEverything = 3; // illegal
constEverything = &j; // illegal
}
In the example there are different types of pointers. With constVal
you can change the address and with constPoint
you can change the underlying value. For constEverything
you can do neither.
To me the purpose of such a pointer would be to pass things by constant reference. Why should I not just use const type &val instead? To me a const reference seems a lot easier and it makes const type* const val obsolete.
回答1:
Constant pointers are useful for two principal reasons.
this
is aconst
pointer in aconst
member function.A
const
pointer can also be set tonullptr
.
My first point is somewhat circular, this
could have been a reference type or a const
reference type, but references arrived late into the C++ standard, and any changes would have been breaking.
回答2:
Pointers are remainders of the C programming language. C didn't have references, thus pointers were your only option. Now you are free to use references in C++, but if you have to work with a C API (e.g. Win32 API) you will have to use pointers.
回答3:
There are two ways to pass arguments: by value, and by reference. Constness is especially relevant when passing by reference: it indicates whether the passed argument can be changed.
In that respect, there are two ways you can pass by reference in C++: using a reference (&) or using a pointer (*). In C, there is only one way to pass by reference: using a pointer (*).
So if you allow passing references as const type &val
in C++, the equivalent in C would have to use pointers, leading to const type * const val
. You could ommit the constness of the pointer itself (which in my experience is often seen in C APIs).
Regardless of personal preferences, since C code should also be valid C++, a pointer based version is required as well.
回答4:
In the following code :
#include <iostream>
struct BigObject {
int a{};
};
BigObject* createBigObj()
{
bool allGood = true ; // some real work here
if ( allGood )
return new BigObject{1};
else
return nullptr;
}
void readBigObj(BigObject const * const bigOb)
{
if (bigOb)
std::cout << bigOb->a << std::endl;
}
int main()
{
auto* big = createBigObj();
readBigObj(big);
return 0;
}
The funciton :
void readBigObj(BigObject const * const bigOb)
Can't take a const BigObject&
because the object can be null. The function didn't change the value of the pointor nor the value, both should be const
Tada! A reason of const value const pointor :)
来源:https://stackoverflow.com/questions/57054001/what-is-the-purpose-of-a-constant-pointer-in-c