Jak zdefiniować tablicę wskaźników const w C++?
Czy istnieje sposób na zdefiniowanie tablicy wskaźników tak, aby każdy wskaźnik był const?
Na przykład, czy char** array
może być zdefiniowane tak, że array[0]
jest const, a array[1]
jest const, itd., ale array
jest non-const i array[j][i]
jest non-const?
2 answers
char* const * pointer;
. then
pointer -> non-const pointer to const pointer to non-const char (char* const *)
pointer[0] -> const pointer to non-const char (char* const)
pointer[0][0] -> non-const char
Jeśli chcesz tablicę to char* const array[42] = { ... };
.
Jeśli nie znasz rozmiaru tablicy w czasie kompilacji i musisz przydzielić tablicę w czasie wykonywania, możesz użyć wskaźnika
int n = ...;
char* const * pointer = new char* const [n] { ... };
...
delete[] pointer;
Jak widzisz, musisz wykonać alokację i dealokację ręcznie. Nawet ty powiedziałeś, że nie chcesz std::vector
ale dla mordern C++ za pomocą std::vector
lub Inteligentne Wskaźniki jest bardziej odpowiednie.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-06-06 01:36:24
Do takiego żądania możesz użyć magicznego narzędzia cdecl
(dostępnego również jako web UI TUTAJ):
$ cdecl -+ %c++ mode
Type `help' or `?' for help
cdecl> declare x as array of const pointer to char
char * const x[]
cdecl>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-06-05 18:28:11