Is it possible to set an object to null?
Asked Answered
C

7

58

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?

Chandigarh answered 13/6, 2010 at 2:12 Comment(0)
T
75

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;
Tiertza answered 13/6, 2010 at 2:13 Comment(6)
as an aside for whoever wants to bring it up, yes you can overload operator= but this is not what the OP wants.Tiertza
The 1st could work with Cat::operator=(...). Anyway, looks like OP really wants to check a pointer. So, +1.Markle
@jweyrich: I knew someone would say that so see my comment before your comment :)Tiertza
sorry, I didn't update the page before posting my comment. It was just for the sake of completeness. Glad you mentioned :)Markle
@wulfgarpro You don't need any includes for nullptr, it's a keyword.Chubb
Correct, cstddef is needed for hash define NULL, which is an implementation defined null pointer constant.Feudalism
C
31

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<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" in any useful sense. Think of std::optional as a container in this regard.

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 should behave at least close to the actual std::optional. Check your compiler's manual for details.

Chubb answered 26/1, 2018 at 1:17 Comment(1)
Or std::experimental::optional if your compiler provides that.Sandhog
D
5

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.

Derry answered 13/6, 2010 at 2:15 Comment(0)
V
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:

  1. In C++ NULL pointer is very different to pointer pointing to an 'empty' object.
  2. 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.
  3. 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.
  4. You may want to do this class Null {}; Null _NULL; and then overload operator= and operator!= of other classes depending on your situation.

Perhaps you should post us some details about the context to help you better with option 4.

Arpan

Vega answered 13/6, 2010 at 3:27 Comment(1)
"Why not have a bool IsEmpty() method ..." - there are some good reasons not to. The most important one is that it's typically contex-dependent whether it makes sense. A more robust solution is to use boost::optional<T> to indicate whether you have a valid T object or not. This will prevent you from calling x.foo() when x.IsEmpty()==trueKrantz
V
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.

If you mean objects of some class type, the answer is NO (excepting classes that have operator= accepting pointer or arithmetic types)

"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==

Vitascope answered 26/1, 2018 at 14:21 Comment(0)
M
-1

in c++14 you can do something like,

MyClass ObjectName = *static_cast<MyClass*>(nullptr);

void* check = &ObjectName;
if(check){**object is NOT null**}
else {**object IS null**}

Intel Inspector doesnt catch any memory errors so I figure its safe-ish, at least good for my needs. As long as you dont dereference ObjectName and only use its address, it works

Moorings answered 1/7 at 22:0 Comment(1)
This shows why you shouldn't rely too much on tools. Dereferencing the null pointer (as on the first line) is Undefined Behavior - anything can happen. You can't "dererence ObjectName" because ObjectName is not a pointer.Krantz
N
-3

yes it is possible to assign null to assign null value to a variable of any data type in many Programing languages with the NULL keyword and with the following method int a="NULL"; here int is the datatype of the variable a and by this method NULL value is assigned to the variable a.

Naker answered 11/7, 2023 at 10:11 Comment(1)
This question was tagged as C++, so this answer "yes" is completely wrong. The qualification "many Programing languages" fails to include the one language which matters. Also, in almost all of those languages, "NULL" is a string because it's quoted, and therefore distinct from a keyword NULL.Krantz

© 2022 - 2024 — McMap. All rights reserved.