The relationship between 0 and null and nullptr in c++11 __c++

Source: Internet
Author: User
Tags define null
1. In essence 1) 0 is the literal value of int type constant 2) NULL is a preprocessing variable, defined in Cstdlib, its value is 0 3) nullptr is an nullptr_t type. The definition of NULL in 2.cstdlib #ifdef __cplusplus #define NULL 0 #else #define NULL ((void *) 0) #endif The reason for this definition is because in the C language
, allowing the void* type to be implicitly convertible to any pointer type, which is not allowed in C + +, but can assign a value of 0 to any type of pointer, so that NULL is defined as 0 in C + +. 
3.0 compared to NULL in the online data, all agree that 0 is better than NULL, because many may be wrong, using 0 more intuitive, such as: void Test (int i) {cout << "in int" << Endl;} void Test (int * i) {cout << "in int *" << Endl} int main () {test (0);//output in int test (NULL);//Output  in int} at this point, two function calls will call test with the parameter int, and the version using the 0 transfer looks more intuitive because 0 itself is a literal constant of type int, and subconsciously thinks NULL should be a pointer type, which is bad for error 4.nullptr than 0. Better nullptr is a nullptr_t type of constant that defines a conversion operator that is going to any pointer type, and does not allow an object of that type to be converted to a pointer type, and in the invocation example above, using nullptr, the second function is called, and the output "in int*", because Nullptr is converted to int *, which is consistent with our usual understanding. The types of nullptr_t can be defined as follows: 5.nullptr_t implementation class nullptr_t {Public:template<class t> inline operator () const//definition Type
        Conversion operator, so that nullptr_t can be converted to any non-class member pointer type {return 0; }//Overload type conversionsoperator so that nullptr_t can be converted to any pointer type in class C (data member pointer/function member pointer)//pointer to a data member in the class, T will be inferred to be the type of the data member Eg:int (X::*); T is int,c x//A pointer to a function member in a class, T is deduced to be the type of the function member Eg:int (X::*) (int); 			
At this point T is equivalent to: typedef int (T) (int) Template<class C, class t> inline operator T C::* () const {return 0;}
Private:void operator& () const;
}; Const NULL_PTR nullptr = {} 6.nullptr How to use the NULLPTR keyword to identify a null pointer, is a std::nullptr_t type (constexpr) variable.
It can be converted to any pointer type and bool Boolean type (primarily to accommodate the normal pointer as a conditional judgment statement), but cannot be converted to an integer.     char *p1 = nullptr;     correct int *p2 = nullptr;       correct bool B = nullptr; That's right.        if (b) is judged to be false int a = nullptr;
    ERROR 7. Test nullptr Union U {Long I;
nullptr_t T;

};
    int main () {u u;
    U.I = 3; printf ("%ld\n", (long) u.t); What it is?
0 or 3? So this is supposed to be in union semantics or nullptr semantics. This is not said in the standard, we have argued for a very long time. Of course, the implementation of our compiler still keeps the semantics of nullptr, and the result is 0.

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.