Look closely, there's only one "=" sign. An equality test would use 2 : "=="
This loop's code uses 2 little tricks rarely taught to beginners:
1) In C, you can test the result of any expression that returns something, and an assignment operation returns the assigned value.
It's often used when you need to check the return value of a function and use that value later. If you could not do that, it would be a little messier to write the same thing.
In short, the loop of your example is the equivalent of this:
1 2 3 4 5 6 7 8 9
|
NameDataSet* pNDS = getData();
while ( pNDS )
{
// add it to the list of NameDataSet objects
add(pNDS);
// create another NameDataSet object
pNDS = getData();
}
|
2) In C/C++, the ifs and while in fact consider the test succeeds if the result of your condition is different from 0. For example, if you write
if( a == b )
,
(a == b)
is in itself an expression that returns 1 if they are equal or 0 otherwise.
As a consequence it's become usual amongst experienced programmers not to explicitly write the condition when it's simply a comparison to 0:
if( a != 0 )
becomes
if( a )
.
Using what I explained in 1), the loop of you example could then be written:
1 2 3 4 5 6 7 8 9
|
NameDataSet* pNDS = getData();
while ( pNDS != 0 // 0 or NULLPTR )
{
// add it to the list of NameDataSet objects
add(pNDS);
// create another NameDataSet object
pNDS = getData();
}
|
It's a bit surprising they used these tricks in a beginner's book without explaining them.
It also makes their definition of NULLPTR useless