I'm pretty new to C++ so I tend to design with a lot of Java-isms while I'm learning. Anyway, in Java, if I had class with a 'search' method that would return an object T
from a Collection< T >
that matched a specific parameter, I would return that object and if the object was not found in the collection, I would return a NULL. Then in my calling function I would just check if(T != NULL) { ... }
In C++, I'm finding out that I can't return a NULL if the object doesn't exist. I just want to return an 'indicator' of type T that notifies the calling function that no object has been found. I don't want to throw an exception because it's not really an exceptional circumstance.
class Node {
....
Attr& getAttribute(const string& attribute_name) const {
//search collection
//if found at i
return attributes[i];
//if not found
return NULL;
}
private:
vector<Attr> attributes;
}