cppLearner
cppLearner

Reputation: 39

Need help to understand C++ syntax

I am relatively new to C++. Here is a if statement I can not understand.

if ((ObjPtr = NewObjPtr) != NULL)
{
    ...
}

What is the "!= NULL" checked for?

Thanks.

Upvotes: 2

Views: 249

Answers (3)

BastiBen
BastiBen

Reputation: 19860

In C++ an expression like myVar = 5 would return 5. So basically this syntax is checking if NewObjPtr is not NULL.

You could say the value of an assignment is passed on to the left.

Upvotes: 1

Vladimir Ivanov
Vladimir Ivanov

Reputation: 43098

Every operand in C(C++) returns the result of the operation. For the '=' operator the result is the value assigned. So, the checking if for NULLness of ObjPrt and NewObjPrt.

Upvotes: 1

vitaut
vitaut

Reputation: 55554

This assigns NewObjPtr to ObjPtr and checks if ObjPtr is non-null.

It is equivalent to the following:

ObjPtr = NewObjPtr;
if (ObjPtr != NULL) { ... }

Upvotes: 11

Related Questions