Dowemo

Copy the address: in the history of, 0, nullptr analysis.

Null of c

In c language, we use null to represent an empty pointer, and that's, we can write the following code:


int *i = NULL;


foo_t *f = NULL;





In a c language, null is typically defined as follows:


#define NULL ((void *)0)





Null is actually a pointer to a void *, and then the void * pointer is implicitly converted to the corresponding type when the pointer is assigned to the pointer to the int * and foo t *. If you're trying to compile a c + + compiler to compile it, it's an error because c + + is strongly typed, and void * isn't implicitly converted to other pointer types, so the compiler provided header files that define null:


#ifdef __cplusplus ---简称:cpp c++ 文件


#define NULL 0


#else


#define NULL ((void *)0)


#endif





C + + 0

Because c + + cannot implicitly convert a pointer to a void * type to another pointer type, I & ert 0 in c + + to represent an empty pointer ( note: 0, or a defect isn't perfect, so there's a similar code to define null. Actually c + + books suggest that c + + is more accustomed to using 0 to represent null pointers, not null, although null is 0 under c + + compilers. Why c + + books are recommended to use 0 instead of null to represent an empty pointer? Let's look at an example:

A function is declared in the foo. H file:


void bar(sometype1 a, sometype2 *b);





This function is called in a. Cpp, b. Cpp, respectively:

A. Cpp:


bar(a, b);





B. Cpp:


bar(a, 0);





All right, the code is perfectly perfect. But suddenly at some point we need to extend the bar function, we use overloading, now foo. H is declared as follows:


void bar(sometype1 a, sometype2 *b);


void bar(sometype1 a, int i);





This is dangerous, and the calling code in a. Cpp and b. Cpp can't run as expected. But we'll soon find that 0 is an integer in b. Cpp, that's, we know that it's called void bar ( a, int I ), so we can make the following modification so that the code can be run as expected:


bar(a, static_cast<sometype2 *>(0)); --- 我们的游戏项目就遇到这个问题,这样用开起来别扭





I know that if we start with the two overloaded functions of bar, we'll try to avoid this problem at first ( without overload ) or we write the correct calling code. It seems that we're now talking about these with c + + normally using 0 to represent null pointers, so let's say that our calling code is like this:

Foo. H.


void bar(sometype1 a, sometype2 *b);





A. Cpp.


bar(a, b);





C. Cpp.


bar(a, NULL);





After the overloaded function of bar is added later, we'll find an error, but we find that the calling code in b. Cpp is also likely to ignore the past because we're using null. Actually null is 0 in c + +, which will let you not alert, because null isn't"obvious", and here if you use 0 to represent an empty pointer, it'll be"obvious"because 0 is an empty pointer, because is an empty pointer.

In c + +, using 0 to make an empty pointer will give you more alert than using null.

Nullptr of c + + 11

Although we show that 0 is more alert than null, we don't have to avoid this problem. In this case, c + + 11 is a good solution to this problem, and we use nullptr in c + + 11 to represent an empty pointer so that the earliest code is so

Foo. H.


void bar(sometype1 a, sometype2 *b);





A. Cpp.


bar(a, b);





C. Cpp.


bar(a, nullptr);





After we've added bar overloading later, the code is like this:

Foo. H.


void bar(sometype1 a, sometype2 *b);


void bar(sometype1 a, int i);





A. Cpp.


bar(a, b);





C. Cpp.


bar(a, nullptr);





At this point, our code can be run correctly as expected.

How do we solve this problem without c + + 11. We can implement one of the following ("imperfect c + +"):

复制代码

const


class nullptr_t


{


public:


 template<class T>


 inline operator T*() const


 { return 0; }



 template<class C, class T>


 inline operator T C::*() const


 { return 0; }



private:


 void operator&() const;


} nullptr = {};





复制代码

Although this thing has been discussed many times, I think it's still necessary to discuss it. After all, we should know how to avoid the problem before c + + 11.





Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs