C++ 是否可以将对象设置为空?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3030829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to set an object to null?
提问by John
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?
有没有办法将对象设置为空?
回答by Brian R. Bondy
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.
类的对象不能设置为 NULL;但是,您可以将指针(包含对象的内存地址)设置为 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;
回答by Baum mit Augen
While it is true that an object cannot be "empty/null" in C++, in C++17, we got std::optional
to express that intent.
虽然在 C++ 中对象不能为“空/空”,但在 C++17 中,我们必须std::optional
表达这种意图。
Example use:
使用示例:
std::optional<int> v1; // "empty" int
std::optional<int> v2(3); // Not empty, "contains a 3"
You can then check if the optional
contains a value with
然后,您可以检查是否optional
包含一个值
v1.has_value(); // false
or
或者
if(v2) {
// You get here if v2 is not empty
}
A plain int
(or any type), however, can never be "null" or "empty" (by your definition of those words) in any useful sense. Think of std::optional
as a container in this regard.
int
但是,在任何有用的意义上,普通(或任何类型)永远不会是“空”或“空”(根据您对这些词的定义)。std::optional
在这方面可以将其视为容器。
If you don't have a C++17 compliant compiler at hand, you can use boost.optionalinstead. Some pre-C++17 compilers also offer std::experimental::optional
, which will behave at least close to the actual std::optional
afaik. Check your compiler's manual for details.
如果您手头没有符合 C++17 的编译器,则可以使用boost.optional代替。一些 C++17 之前的编译器还提供std::experimental::optional
,其行为至少接近实际的std::optional
afaik。有关详细信息,请查看您的编译器手册。
回答by Fanatic23
You want to check if an object is NULL/empty. Being NULL and empty are not the same. Like Justin and Brian have already mentioned, in C++ NULL is an assignment you'd typicallyassociate with pointers. You can overload operator= perhaps, but think it through real well if you actually want to do this. Couple of other things:
您想检查对象是否为 NULL/空。NULL 和空是不一样的。就像 Justin 和 Brian 已经提到的那样,在 C++ 中,NULL 是一个通常与指针相关联的赋值。也许您可以重载 operator=,但如果您真的想这样做,请仔细考虑一下。其他几件事:
- In C++ NULL pointer is very different to pointer pointing to an 'empty' object.
- Why not have a
bool IsEmpty()
method that returns true if an object's variables are reset to some default state? Guess that might bypass the NULL usage. - Having something like
A* p = new A; ... p = NULL;
is bad (no delete p) unless you can ensure your code will be garbage collected. If anything, this'd lead to memory leaks and with several such leaks there's good chance you'd have slow code. - You may want to do this
class Null {}; Null _NULL;
and then overload operator= and operator!= of other classes depending on your situation.
- 在 C++ 中,NULL 指针与指向“空”对象的指针非常不同。
bool IsEmpty()
如果对象的变量被重置为某个默认状态,为什么没有一个返回 true的方法呢?猜测这可能会绕过 NULL 用法。- 拥有类似的东西
A* p = new A; ... p = NULL;
是不好的(不删除 p),除非您可以确保您的代码将被垃圾收集。如果有的话,这会导致内存泄漏,并且有几个这样的泄漏很有可能你的代码很慢。 - 您可能希望这样做
class Null {}; Null _NULL;
,然后根据您的情况重载其他类的 operator= 和 operator!=。
Perhaps you should post us some details about the context to help you better with option 4.
也许您应该向我们发布一些有关上下文的详细信息,以帮助您更好地使用选项 4。
Arpan
阿潘
回答by Justin Ardini
You can set any pointer to NULL
, though NULL
is simply defined as 0 in C++:
您可以设置任何指向 的指针NULL
,但NULL
在 C++ 中只是简单地定义为 0:
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:
另请注意,NULL
如果包含标准标头,则已定义,但未内置到语言本身中。如果NULL
未定义,则可以使用 0 代替,或包括以下内容:
#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.
顺便说一句,如果你真的想设置一个对象,而不是一个指针,到NULL
,你可以阅读Null Object Pattern。
回答by Caleth
"an object" of what type?
什么类型的“对象”?
You can certainly assign NULL
(and nullptr
) to objects of pointer types, and it is implementation defined if you can assign NULL
to objects of arithmetic types.
您当然可以将NULL
(和nullptr
)分配给指针类型的对象,并且如果您可以分配NULL
给算术类型的对象,则它是实现定义的。
If you mean objects of some class type, the answer is NO(excepting classes that have operator=
accepting pointer or arithmetic types)
如果您指的是某种类类型的对象,则答案是否定的(operator=
接受指针或算术类型的类除外)
"empty" is more plausible, as many types have both copy assignment and default construction (often implicitly). To see if an existing objectis like a default constructed one, you will also need an appropriate bool operator==
“空”更合理,因为许多类型同时具有复制赋值和默认构造(通常是隐式的)。要查看现有对象是否类似于默认构造的对象,您还需要一个适当的bool operator==