J
John J
The following code is complete and working within a class; however, it uses
"e" as a pointer to an Entry object without checking if that it is not null.
This of course doesn't matter if objects are created correctly; however, it
will probably cause a problem should the "e" be null. Is there a simple
means I can use to check that "e" is not null?
Thanks for any help
//Find the winner
void Race::winner (ostream& out) const
{
Entry* e = NULL;
if (nEntries == 0)
{
out << "No entries" << endl;
}
else
{
bool found_a_winner = false; // no winner found
yet
for (int i=0; i<nEntries; i++)
{
e = entries;
if (e->getPlace() == 1)
{
out << "The winner is: " << endl;
out << *e;
found_a_winner = true; // a winner has been
found
}
}
if (!found_a_winner) // if no winner
{
out << "No winner was found in this
race" << endl;
}
}
}
"e" as a pointer to an Entry object without checking if that it is not null.
This of course doesn't matter if objects are created correctly; however, it
will probably cause a problem should the "e" be null. Is there a simple
means I can use to check that "e" is not null?
Thanks for any help
//Find the winner
void Race::winner (ostream& out) const
{
Entry* e = NULL;
if (nEntries == 0)
{
out << "No entries" << endl;
}
else
{
bool found_a_winner = false; // no winner found
yet
for (int i=0; i<nEntries; i++)
{
e = entries;
if (e->getPlace() == 1)
{
out << "The winner is: " << endl;
out << *e;
found_a_winner = true; // a winner has been
found
}
}
if (!found_a_winner) // if no winner
{
out << "No winner was found in this
race" << endl;
}
}
}