2

It's not clear for me how can i do that in C++. In Objective-C I can check a object in this way

if (myValue != [NSNull null]) { … }

myValue is compared with a null object (returned by class method), so this works great , if object has a value, even nil, if statement will return true.

So question is how to test correctly for a null pointer value, i did this way

if (myValue != NULL)
{
    qDebug() << "It is not null";
}

but it is not working.

8
  • What is the type of myValue? Can you add more code please? Commented Dec 15, 2011 at 12:57
  • 2
    There is no such thing as a null object in C++. You can have null pointers, though. Commented Dec 15, 2011 at 12:58
  • There's also Boost.Optional, if you need a nullable object type. Commented Dec 15, 2011 at 12:59
  • 1
    @devXcode Objects (class instances) always have values - they may be uninitialized or zero, but there's always some value. You can have a null pointer, which just doesn't point to any value (but itself has value NULL). Commented Dec 15, 2011 at 13:03
  • 1
    @devXcode: In C++, there is no concept of an object without a value. You can use a pointer, or a higher level construct such as boost::optional, to emulate such a thing, but all objects have values. Commented Dec 15, 2011 at 13:03

4 Answers 4

5

In C++ there's really no concept of null value, only null pointers. You can't compare something that isn't a pointer to NULL.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 Though you actually can make the comparison, against and integer for example. But it just doesn't make sense.
@AntonioPérez According to the standard, you can compare any numeric type with NULL. Comparing something other than a pointer, however, is misleading, and some compilers will issue a warning if you do.
0

A pointer in C++ essentially contains an address to some memory location where you object or data is stored. In this case, a "NULL Pointer" is just an empty memory address. This is represented as a zero value. So to check it, you would write something like this:

SomeClass *someClassPointer = // ... call some method to get pointer

// Check for null pointer
if (someClassPointer == 0)
{
    // Pointer is null
}

This can be simplified by doing this:

if (someClassPointer)
{
    // For checking that the pointer is not null
}

if (!someClassPointer)
{
    // For checking that a pointer is null
}

5 Comments

A "null pointer" isn't an empty memory address; it's a very special address which is guaranteed to point to no object (in the context of C++). And it isn't necessarily represented as a zero value (although this is frequent); what is guaranteed is that an integral constant expression evaluating to 0 will be converted to a null pointer in pointer contexts.
@JamesKanze what you say was true in old C. It is no longer exact in C++ and standard C, cf. Bjarne Stroustrup www2.research.att.com/~bs/bs_faq2.html#null
@Offirmo What I said is true for C++98, C++03, C++11, C90 and C99. And I don't see anything in the link you give which disagrees with what I just said. Bjarne is just discussing his preference for using 0 instead of NULL (which must be defined to evaluate to "an integral constant expression with value 0").
@JamesKanze sorry, I wasn't precise enough. I was refering only to the part "it isn't necessarily represented as a zero value (although this is frequent)" which I used to believe, too.
@Offirmo It may be me that's not very precise: what I meant is that a null pointer is not necessarily all 0 bits. A null pointer constant must be an integral constant expression (thus not a pointer) which evaluates to 0. When you convert it to a pointer type, the compiler does whatever it has to for it to be a null pointer, regardless of the representation of a null pointer.
0

Short answer: it depends.

Longer answer: First of all you can not compare a reference (if the type of myValue is something like T&) nor stack allocated objects (if myValue is just T) for null - these types are always allocated and not null (unless you screw up your stack or do other bad stuff - but you won't check for these cases, because than you will have bigger problems).

The only types you can check for null are pointers (myValue is something of type T*). For those types:

  • if you use C++98, you can check against 0. NULL is usually just a macro (#define NULL 0).
  • if you use the new C++11 there is a new nullptr keyword and you should check against this one.

Since 0 evaluates to false and anything else to true, you can also just use the pointer like a normal bool. A nullptr is of type nullptr_t and has operator bool() implemented - so you also can this one like you would use a bool.

In general: in C++98 the lack of nullptr is a source of errors, so if you can use C++11, ALWAYS use nullptr - never use NULL or 0 (int is just the wrong type to assign to a pointer or to compare with a pointer - if you have overladed methods you will run into problems since the compiler would use a method with an int parameter instead of a pointer type if it is suitable).

Let's assume you have two functions:

void foo(int a);

void foo(void *);

If you now call

foo(NULL);

the first function will get called (what probably is not what you want. So in C++11 you would write:

foo(nullptr);

while in C++98 you would have to write:

foo((void *)0);

This is one reason why the lack of null is/was a big issue before C++98. If you want to have something similar than in Objective-C, you could write the following function (for C++98):

template<typename T>
inline bool isNull(const T *obj) {
  return obj == ((const T *) 0);
}

// to use it:
if (isNull(myType)) { //myType is a pointer to something
  // some code
}

Although I never saw that one used in practice.

I hope this answer helps in understanding the concept in C++.

1 Comment

In C++90, you can always compare with NULL; the macro is guaranteed to evaluate to a null pointer constant.
0

If i'm right, you want to check for null pointers. This is actually very easy in C++

Foo *pInstanc = new Foo;
if(pInstance)
{
    //do something with pInstance
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.