Is it possible to set an object to null?

前端 未结 5 1899
一个人的身影
一个人的身影 2020-12-01 07:39

Further in my code, I check to see check if an object is null/empty.

Is there a way to set an object to null?

5条回答
  •  佛祖请我去吃肉
    2020-12-01 08:09

    You can set any pointer to NULL, though NULL is simply defined as 0 in C++:

    myObject *foo = NULL;
    

    Also note that NULL is defined if you include standard headers, but is not built into the language itself. If NULL is undefined, you can use 0 instead, or include this:

    #ifndef NULL
    #define NULL 0
    #endif
    

    As an aside, if you really want to set an object, not a pointer, to NULL, you can read about the Null Object Pattern.

提交回复
热议问题