Nullptr
nullptr — ключевое слово, введенное в C++11 для описания константы нулевого указателя. Её тип — std::nullptr_t.
nullptr является r-value[англ.] литералом[1].
До введения nullptr для обнуления указателей использовался макрос NULL со значением 0 — целым типом. Это вызывало проблемы (например, при перегрузке функций). Тип nullptr — std::nullptr_t, что решает эти проблемы. Существуют неявные преобразования nullptr к нулевому указателю любого типа и к bool (со значением false), но не к целочисленных типам[2].
В целях обеспечения обратной совместимости константа 0 также может использоваться в качестве нулевого указателя.
void foo(char *);
void foo(int);
char *pc = nullptr; // верно
int *pi = nullptr; // верно
bool b = nullptr; // верно. b = false.
int i = nullptr; // ошибка
foo(nullptr); // вызывает foo(char *), а не foo(int);
Примечания
- ↑ cppreference.com: nullptr. https://ravesli.com (Обновл. 31 Дек 2019). Дата обращения: 14 ноября 2022. Архивировано 14 ноября 2022 года.
- ↑ Renzo. Десять возможностей C++11, которые должен использовать каждый C++ разработчик. habr (11 июня 2013). Дата обращения: 10 января 2020. Архивировано 2 апреля 2022 года.
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.