Well, since it's C ++, we can go crazy looking for proximity ... for example:
int count = !!temp1 + !!temp2 + !!temp3;
Update: I probably should explain to Ivan what is happening here.
Assuming temp is any type of pointer !temp forces the pointer to be forced to bool (we want to do this) and negates the result (this is a side effect that we don’t need), This leads to true if the pointer is null and false , if the pointer is not null, which is the opposite of what we would like. Therefore, we will add one more ! to the beginning to deny the result again.
This leaves us with the addition of three bool values, which force them to int and perform the addition, after which we get our final result.
It may be easier for you to understand the fully equivalent
int count = (bool)temp1 + (bool)temp2 + (bool)temp3;
which I did not use because input !! three characters shorter (bool) (note: you might think this is a good trick, but when writing code it's really a bad idea to make decisions based on how many characters you have to enter).
The moral of this story is that such a thing can be called smart or cruel, depending on who you ask, but in C ++ there has traditionally been a high tolerance for atrocities.
Note that if the pointers were in some type of collection to start with, you could write much more beautiful code using std::count_if , for example:
bool isNotNull(void* ptr) { return ptr != 0; } std::vector<Some_class*> vec; vec.push_back(temp1); vec.push_back(temp2); vec.push_back(temp3); int count = std::count_if(vec.begin(), vec.end(), isNotNull);
Look at the action .
Or, as MSalters suggested very reasonably in the comments, you can lose the isNotNull function by counting pointers that are 0 and subtract this from all the pointers - but for this you need to know somehow what the number is (easy if they are in vector ):
int count = vec.size() - std::count(vec.begin(), vec.end(), 0);
Look at the action .