In dynamically typed languages like JavaScript or PHP, I often do functions such as:
function getSomething(name) { if (content_[name]) return content_[name]; return null; // doesn't exist }
I return an object if it exists or null
if not.
What would be the equivalent in C++ using references? Is there any recommended pattern in general? I saw some frameworks having an isNull()
method for this purpose:
SomeResource SomeClass::getSomething(std::string name) { if (content_.find(name) != content_.end()) return content_[name]; SomeResource output; // Create a "null" resource return output; }
Then the caller would check the resource that way:
SomeResource r = obj.getSomething("something"); if (!r.isNull()) { // OK } else { // NOT OK }
However, having to implement this kind of magic method for each class seems heavy. Also it doesn't seem obvious when the internal state of the object should be set from "null" to "not null".
Is there any alternative to this pattern? I already know it can be done using pointers, but I am wondering how/if it can be done with references. Or should I give up on returning "null" objects in C++ and use some C++-specific pattern? Any suggestion on the proper way to do that would be appreciated.
You cannot do this during references, as they should never be NULL.
Generally NULL literal means that pointer is pointing to nothing - nothing is conventionally represented by value 0 . Whatever it will be, C or C++ . Show activity on this post. When you return NULL, or anything else, the return value is likely saved on the stack (or in a CPU register).
No. return is used to "break" out from a function that has no return value, i.e. a return type of void . return NULL returns the value NULL , and the return type of the function it's found in must be compatible with NULL .
C # in TeluguA C++ function can return a reference in a similar way as it returns a pointer. When returning a reference, be careful that the object being referred to does not go out of scope. So it is not legal to return a reference to local var. But you can always return a reference on a static variable.
You cannot do this during references, as they should never be NULL. There are basically three options, one using a pointer, the others using value semantics.
With a pointer (note: this requires that the resource doesn't get destructed while the caller has a pointer to it; also make sure the caller knows it doesn't need to delete the object):
SomeResource* SomeClass::getSomething(std::string name) { std::map<std::string, SomeResource>::iterator it = content_.find(name); if (it != content_.end()) return &(*it); return NULL; }
Using std::pair
with a bool
to indicate if the item is valid or not (note: requires that SomeResource has an appropriate default constructor and is not expensive to construct):
std::pair<SomeResource, bool> SomeClass::getSomething(std::string name) { std::map<std::string, SomeResource>::iterator it = content_.find(name); if (it != content_.end()) return std::make_pair(*it, true); return std::make_pair(SomeResource(), false); }
Using boost::optional
:
boost::optional<SomeResource> SomeClass::getSomething(std::string name) { std::map<std::string, SomeResource>::iterator it = content_.find(name); if (it != content_.end()) return *it; return boost::optional<SomeResource>(); }
If you want value semantics and have the ability to use Boost, I'd recommend option three. The primary advantage of boost::optional
over std::pair
is that an unitialized boost::optional
value doesn't construct the type its encapsulating. This means it works for types that have no default constructor and saves time/memory for types with a non-trivial default constructor.
I also modified your example so you're not searching the map twice (by reusing the iterator).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With