stdoptional

Is it possible to set an object to null?

眉间皱痕 提交于 2019-11-27 07:57:57
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? 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; While it is true that an object cannot be "empty/null" in C++, in C++17, we got std::optional to express that intent. Example use: std::optional

Is it possible to set an object to null?

笑着哭i 提交于 2019-11-26 13:56:24
问题 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? 回答1: 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; 回答2: While it is true that an object