C++11---nullptr

Source: Internet
Author: User

"1" About 0 and NULL

"2" on the nullptr and void* of the law

Nullptr habits are called pointer null values

Void* habits are called untyped pointers

"3" nullptr and nullptr_t

Nullptr habits are called pointer null values

The nullptr_t habit is called the pointer null type

That is, the pointer null value type is not just one instance of nullptr

You can declare a variable with a pointer to a null value type by nullptr_t

Except for nullptr and nullptr_t, there are various built-in types in C + +.

The C++11 standard strictly specifies the relationship between the data. The common rules are simple as follows:

1. All data defined as nullprt_t type is equivalent and behaves exactly the same

2. nullprt_t type data can be implicitly converted to any pointer type

3. nullptr_t type data cannot be converted to a non-pointer type.

Even using reinterpret_cast<nullptr_t> is not possible.

4. nullptr_t type data does not apply to arithmetic operation expressions

5. nullptr_t type data can be used for relational operation expressions,

But can only be compared with nullptr_t type data or pointer type data

Returns True when and only if the relational operator is ==,<=,>=, etc.

The sample code is as follows:

1#include <iostream>2 using namespacestd;3 4 voidFunintN) {cout <<"Fun (int n):"<< N <<Endl;};5 voidFunint*p) {cout <<"Fun (int *p):"<< P <<Endl;};6 7 voidMain ()8 {9     intNzero =0;Ten     intNvalue = NULL;//OK One     int*pzero =0;//OK A     int*pnull = NULL;//OK -     int*pnullptr = nullptr;//OK -     void*pempty = nullptr;//OK the  - //int ntemp = nullptr; //Error -     /* - the compilation error message for this statement is as follows: + error C2440: "Initialize": Cannot convert from "nullptr" to "int" - native nullptr can only be converted to bool or converted to integers using reinterpret_cast +     */ A     BOOLBIs =nullptr; at     intNtemp = reinterpret_cast<int>(nullptr); -  -Fun0);//-Fun (int n): 0 -Fun (NULL);//-->fun (int n): 0 -Fun ((int*)0);//-->fun (int *p): 00000000 -Fun (nullptr);//-->fun (int *p): 00000000 in  - nullptr_t pnull; toFun (Pnull);//-->fun (int *p): 00000000 +  -     Char* Pchar =Pnull; the     int* PInt =Pnull; *  $ //int n1 = pnull;Panax Notoginseng //int n2 = reinterpret_cast<int>pnull; -  the     if(0==nullptr) +     {} A //if (nullptr) the //    {} +  - //nullptr + = 1; $ //Pnull * TEN; $  -cout <<sizeof(nullptr) << Endl;//4 -}

Good good Study, day.

Sequential Selection Cycle Summary

C++11---nullptr

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.