Quaxton Hale
Quaxton Hale

Reputation: 2520

Compare via pointers

I would like some explanation as to what a part of this function does:

bool Compare(CBox* pBox) const
    {
        if (!pBox)
            return 0;
        return this->Volume() > pBox->Volume();
    }

What does if(!pBox) check for? Is that if statement necessary?

Upvotes: 1

Views: 76

Answers (2)

Erick
Erick

Reputation: 1022

The IF is testing for null, if it is true (not zero), it ensure a zero being returned. It is necessary since you are comparing an instantiated object (you are calling its method) against another and this last can be null.

Upvotes: 2

lolando
lolando

Reputation: 1751

if (!pBox) checks if the pointer pBox is null. It is necessary since you are calling a function (Volume()).

Upvotes: 8

Related Questions