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 个解决方案
#1
38
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;
#2
6
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::可选来表达这个意图。
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
然后,您可以检查可选项是否包含一个值
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(或任何类型)在任何有用的意义上都不能是“null”或“empty”(根据您对这些词的定义)。可以将std::可选作为容器来考虑。
If you don't have a C++17 compliant compiler at hand, you can use boost.optional instead. 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。可选的。一些pre- c++ 17编译器也提供std::experimental:::optional,它的行为至少接近于实际的std::optional afaik。查看编译器手册以获取详细信息。
#3
4
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 typically associate with pointers. You can overload operator= perhaps, but think it through real well if you actually want to do this. Couple of other things:
您需要检查一个对象是否为空。空和空是不一样的。就像Justin和Brian已经提到的,在c++中,NULL是一个你通常会联想到指针的任务。你可以重载运算符=也许,但是如果你真的想这样做的话,请仔细想想。一些其他的东西:
- In C++ NULL pointer is very different to pointer pointing to an 'empty' object.
- 在c++中,空指针与指向“空”对象的指针非常不同。
- 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. - 为什么不让bool IsEmpty()方法返回true,如果对象的变量被重置为某种默认状态?我猜这可能会绕过零使用。
- 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. - 有一个* p =新的A;…p =零;是坏的(不删除p)除非你能确保你的代码被垃圾收集。如果有的话,这将导致内存泄漏,如果有几个这样的泄漏,那么很可能会出现代码缓慢的情况。
- You may want to do this
class Null {}; Null _NULL;
and then overload operator= and operator!= of other classes depending on your situation. - 您可能想要执行这个类Null {};零_NULL;然后重载运算符=和运算符!=根据你的情况。
Perhaps you should post us some details about the context to help you better with option 4.
也许您应该发布一些有关上下文的细节,以帮助您更好地使用选项4。
Arpan
Arpan
#4
3
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对象模式的内容。
#5
0
"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)
如果您指的是某个类类型的对象,那么答案是NO(除了具有操作符=接受指针或算术类型的类)
"empty" is more plausible, as many types have both copy assignment and default construction (often implicitly). To see if an existing object is like a default constructed one, you will also need an appropriate bool operator==
“empty”更合理,因为许多类型同时具有复制赋值和默认构造(通常是隐式的)。要查看现有对象是否类似于默认构造的对象,还需要适当的bool操作符==
#1
38
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;
#2
6
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::可选来表达这个意图。
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
然后,您可以检查可选项是否包含一个值
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(或任何类型)在任何有用的意义上都不能是“null”或“empty”(根据您对这些词的定义)。可以将std::可选作为容器来考虑。
If you don't have a C++17 compliant compiler at hand, you can use boost.optional instead. 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。可选的。一些pre- c++ 17编译器也提供std::experimental:::optional,它的行为至少接近于实际的std::optional afaik。查看编译器手册以获取详细信息。
#3
4
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 typically associate with pointers. You can overload operator= perhaps, but think it through real well if you actually want to do this. Couple of other things:
您需要检查一个对象是否为空。空和空是不一样的。就像Justin和Brian已经提到的,在c++中,NULL是一个你通常会联想到指针的任务。你可以重载运算符=也许,但是如果你真的想这样做的话,请仔细想想。一些其他的东西:
- In C++ NULL pointer is very different to pointer pointing to an 'empty' object.
- 在c++中,空指针与指向“空”对象的指针非常不同。
- 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. - 为什么不让bool IsEmpty()方法返回true,如果对象的变量被重置为某种默认状态?我猜这可能会绕过零使用。
- 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. - 有一个* p =新的A;…p =零;是坏的(不删除p)除非你能确保你的代码被垃圾收集。如果有的话,这将导致内存泄漏,如果有几个这样的泄漏,那么很可能会出现代码缓慢的情况。
- You may want to do this
class Null {}; Null _NULL;
and then overload operator= and operator!= of other classes depending on your situation. - 您可能想要执行这个类Null {};零_NULL;然后重载运算符=和运算符!=根据你的情况。
Perhaps you should post us some details about the context to help you better with option 4.
也许您应该发布一些有关上下文的细节,以帮助您更好地使用选项4。
Arpan
Arpan
#4
3
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对象模式的内容。
#5
0
"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)
如果您指的是某个类类型的对象,那么答案是NO(除了具有操作符=接受指针或算术类型的类)
"empty" is more plausible, as many types have both copy assignment and default construction (often implicitly). To see if an existing object is like a default constructed one, you will also need an appropriate bool operator==
“empty”更合理,因为许多类型同时具有复制赋值和默认构造(通常是隐式的)。要查看现有对象是否类似于默认构造的对象,还需要适当的bool操作符==