Is it possible to set an object to null?

前端 未结 5 1893
一个人的身影
一个人的身影 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:01

    An object of a class cannot be set to NULL; however, you can set a pointer (which contains a memory address of an object) to NULL.

    Example of what you can't do which you are asking:

    Cat c;
    c = NULL;//Compiling error
    

    Example of what you can do:

    Cat c;
    //Set p to hold the memory address of the object c
    Cat *p = &c;
    //Set p to hold NULL
    p = NULL;
    

提交回复
热议问题