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 935395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:09:01+00:00 2026-05-15T21:09:01+00:00

So I’m having a rather tumultuous conversion to C++ from Java/C#. Even though I

  • 0

So I’m having a rather tumultuous conversion to C++ from Java/C#. Even though I feel like I understand most of the basics, there are some big fat gaping holes in my understanding.

For instance, consider the following function:

Fruit&
FruitBasket::getFruitByName(std::string fruitName)
{
    std::map<std::string,Fruit>::iterator it = _fruitInTheBascit.find(fruitName);
    if(it != _fruitInTheBascit.end()) 
    {
        return (*it).second;
    }
    else
    {
           //I would so love to just return null here
    }

}

Where _fruitsInTheBascit is a std::map<std::string,Fruit>. If I query getFruitByName("kumquat") you know it’s not going to be there – who eats kumquats? But I don’t want my program to crash. What should be done in these cases?

P.S. tell me of any other stupidity that I haven’t already identified.

  • 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-05-15T21:09:01+00:00Added an answer on May 15, 2026 at 9:09 pm

    There is no such thing in C++ as a null reference, so if the function returns a reference, you can’t return null. You have several options:

    1. Change the return type so that the function returns a pointer; return null if the element is not found.

    2. Keep the reference return type but have some sort of “sentinel” fruit object and a return a reference to it if the object is not found.

    3. Keep the reference return type and throw an exception (e.g., FruitNotFoundException) if the fruit is not found in the map.

    I tend to use (1) if a failure is likely and (3) if a failure is unlikely, where “likely” is a completely subjective measure. I think (2) is a bit of a hack, but I’ve seen it used neatly in some circumstances.

    As an example of an “unlikely” failure: in my current project, I have a class that manages objects and has a function is_object_present that returns whether an object is present and a function get_object that returns the object. I always expect that a caller will have verified the existence of an object by calling is_object_present before calling get_object, so a failure in this case is quite unlikely.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to count the length of a string with PHP. The string
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.