Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7871181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:49:50+00:00 2026-06-03T01:49:50+00:00

In dynamically typed languages like JavaScript or PHP, I often do functions such as:

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T01:49:53+00:00Added an answer on June 3, 2026 at 1:49 am

    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.

    1. 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;  
      }
      
    2. 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);  
      }
      
    3. 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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In terms of quick dynamically typed languages, I'm really starting to like Javascript, as
Whenever I write functions in dynamically-typed languages, I'm still torn as to what is
In Objective-C, I usually see methods that return a dynamically typed object defined as
I am writing a Function that has a return-type which will dynamically change depending
With the growth of dynamically typed languages, as they give us more flexibility, there
I see that in Ruby (and dynamically typed languages, in general) a very common
Objective-C is a language like Smalltalk, but weakly, dynamically typed language. And I can
Which computer languages will allow code that dynamically extracts a variable name from a
I am dynamically changing the cursor type on a Google Map using the Javascript
Let's say I have some HTML and Javascript which adds text fields dynamically to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.