*
とを配置する正確な正しい位置は何かといつも思っていました&
。C++ は、これらのマークをどこに置くかについてかなり寛容であるようです。たとえば、ポインタとアンパサンドがキーワードの左右または 2 つのキーワードの中間に置かれているように見えますが、紛らわしいことに、特に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);
例はすべてを網羅しているわけではありません。宣言されている間、または関数に渡されている間、どこでもそれらを見ます。彼らは同じことを意味していますか?しかし、パッティング*
がポインターとして参照されるオブジェクトに影響を与える場合もあります ( *
is が 2 つのキーワードの間にある場合など)。
編集:
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
だから私はこれを結論付けました:
T const* p; // pointer to const T
const T* p // seems same from above
T* const p; // const pointer to T
それでも、これは私を混乱させました。コンパイラは、それらに必要な位置と間隔を気にしませんか?
編集:一般的にポジションの問題を知りたいです。はいの場合、どのような場合に。