Misunderstanding in some if statements...

What does this expression mean?

1
2
3
4
5
6
IDrawable *pDrawable = CreateTriangleShape();

if (pDrawable)
{
    pDrawable->Draw();
}
Do you know how the if statement works?
Yes, but what does the underlined part mean. Can you explain in pure English?
it just checks if the pDrawable pointer is true or false - in C++ a non-zero value (for built in types) constitues true and a zero value is false.

so if pDrawable is non-zero (true) - it meant that the CreateTriangleShape() function returned a valid (non-zero) pointer
and the pDrawable->Draw() statement will be executed.

if CreateTriangleShape() could NOT create the triangle object it would return a zero value which would mean that if (pDrawable) would test as false and the pDrawable->Draw() statement would NOT be executed.
Thank you very much. I now understand!!!
Normally a zero pointer is called a null pointer. To make it more explicit the if statement could have been written in one of the following ways.
1
2
3
if (pDrawable != NULL)
if (pDrawable != 0)
if (pDrawable != nullptr)

@Peter87

Yeah I know that one. Thanks!
Topic archived. No new replies allowed.