Neredeyse diğer yanıtların tümü doğrudur, ancak bunun bir yönünü gözden kaçırırlar: constBir fonksiyon bildirimindeki bir parametrede fazladan kullandığınızda , derleyici bunu esasen yok sayar. Bir an için, örneğinizin işaretçi olmasının karmaşıklığını görmezden gelelim ve sadece bir int.
void foo(const int x);
ile aynı işlevi ilan eder
void foo(int x);
Yalnızca fonksiyonun tanımında ekstra constanlamlıdır:
void foo(const int x) {
// do something with x here, but you cannot change it
}
Bu tanım, yukarıdaki beyanlardan herhangi biriyle uyumludur. Arayan kişi bunun umurunda değil - bu x, constarama sitesinde alakalı olmayan bir uygulama detayıdır.
Verilere bir constişaretçiniz varsa const, aynı kurallar geçerlidir:
// these declarations are equivalent
void print_string(const char * const the_string);
void print_string(const char * the_string);
// In this definition, you cannot change the value of the pointer within the
// body of the function. It's essentially a const local variable.
void print_string(const char * const the_string) {
cout << the_string << endl;
the_string = nullptr; // COMPILER ERROR HERE
}
// In this definition, you can change the value of the pointer (but you
// still can't change the data it's pointed to). And even if you change
// the_string, that has no effect outside this function.
void print_string(const char * the_string) {
cout << the_string << endl;
the_string = nullptr; // OK, but not observable outside this func
}
Çok az C ++ programcısı const, bu parametrelerin işaretçi olup olmadığına bakılmaksızın, olabildiğinde bile parametre oluşturmaya zahmet eder .