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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:57:42+00:00 2026-06-05T07:57:42+00:00

Apologies for my improper terminology. I have a piece of code that returns a

  • 0

Apologies for my improper terminology.

I have a piece of code that returns a NULL pointer if an entry doesn’t exist:

ObjectType * MyClass::FindObjectType( const char * objectTypeName )
{
    if ( objectTypeMap.find( objectTypeName ) == objectTypeMap.end() )
    {
        Msg( "\n[C++ ERROR] No object type: %s", objectTypeName );
        return NULL;
    }
    else
        return &objectTypeMap[ objectTypeName ];
}

I want to do the same thing but this time returning an object instead of just a pointer. The following code isn’t giving me any compiler errors (which surprises me):

ObjectType MyClass::FindObjectType( const char * objectTypeName )
{
    if ( objectTypeMap.find( objectTypeName ) == objectTypeMap.end() )
    {
        Msg( "\n[C++ ERROR] No object type: %s", objectTypeName );
    }
    else
        return objectTypeMap[ objectTypeName ];
}

With the pointer I can check if the entry wasn’t found like so:

if ( FindObjectType( objectType ) == NULL )
    //Do something

How do I perform the equivalent check with the object being returned?

  • 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-05T07:57:45+00:00Added an answer on June 5, 2026 at 7:57 am

    There is no language-level equivalent for objects.

    One option is to create a “sentinel” object that is guaranteed to compare unequal to any “real” object, and return that:

    class ObjectType {
    public:
        static const ObjectType null;
    
        bool operator==(const ObjectType &rhs) const { /* need an appropriate comparison test */ }
    
        ...
    };
    
    ObjectType ObjectType::null(/* something unique */);
    
    
    ...
    
    ObjectType foo(const char *objectTypeName) {
        if (cond) {
            return objectTypeMap[objectTypeName];
        } else {
            return ObjectType::null;
        }
    }
    
    
    ...
    
    if (foo(objectType) == ObjectType::null) {
        std::cout << "Returned the null object\n";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Apologies if this question has been asked already, but suppose we have this code
Apologies if this was already asked but, I have had a look and can't
Apologies for a simple question, but I'm very new to Perl! I have an
Apologies if this has been answered previously. Say I have the following table: colA
Apologies if this doesn't make any sense, I'm very new to Python! From testing
Apologies if this is trivial. I was trying to run this code - http://oreilly.com/pub/h/974#code
Apologies but my knowledge of url rewriting is limited! I have setup urls to
Apologies in advance for the length of this question! I have a data structure
Apologies if I am not wording this correctly but what I have at the
Apologies in advance, because I suspect this may be a silly question. I have

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.