関数の宣言と定義がパラメータの定数に一致しない場合があります。私はいくつかの情報(次のリンク)を見つけましたが、私の質問はなぜconstでバイナリパラメータのオプションマッチングですが、constマッチングは参照パラメータに必要ですか?関数のパラメータ:const matching宣言と定義
here次のコードがあるとします。
class MyClass
{
int X;
int Y;
int Z;
public:
void DoSomething(int z, int y, const int& x);
int SomethingElse(const int x);
void Another(int& x);
void YetAnother(const int& z);
};
void MyClass::DoSomething(int z, const int y, const int& x) // const added on 2nd param
{
Z = z;
Y = y;
X = x;
}
int MyClass::SomethingElse(int x) // const removed from param
{
X = x;
x = 3;
return x;
}
void MyClass::Another(int& x) // const not allowed on param
{
X = x;
}
void MyClass::YetAnother(const int& z) // const required on param
{
Z = z;
}
私はthis on SOを見つけたが、それは名前の符号化の説明を探しています。私はthis on SOとthis on SOも見つけましたが、参照パラメータにconstマッチングが必要な理由について詳しくは説明しません。
ミスマッチについては、[こちら](http://stackoverflow.com/q/20659000/1460794)で説明しています。 – wally
これは重複する質問ではありません。私は価値観と参考文献の両方に関するconstと、なぜ**の理由について違いがあるのかについての質問は見つかりませんでした。 [this](http://stackoverflow.com/questions/117293/use-of-const-for-function-parameters)には参照によってカバーされている回答がありますが、それは質問が副次的なものであることから明らかではありませんでしたが、 **なぜ**違いがありますか? – bamakid