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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:49:11+00:00 2026-06-13T14:49:11+00:00

I have a member function with an object type as the return value type:

  • 0

I have a member function with an object type as the return value type:

MyObject myfunction(parameters) {
    if (some condition) { 
        return MyObject(parameters);
    } else { 
        ... no valid object can be created ... 
    } 
}

Under certain conditions (checked for in the function body) an object of type MyObject cannot be created and returned.

Beeing just an occasional c++ programmer I can spontaneously come up with three solutions:

  1. Changing the return value type to * MyObject and returning nullptr if no valid object can be created (C++11), then checking for equality to nullptr in the calling code.
  2. Throwing an exception if no object can be created and catching that one in the calling code.
  3. Creating an object with some values that I define as invalid and check for that before using the returned object.

What would be the standard way of dealing with such a situation and the best solution in terms of performance? … or some obvious work-around that I just don’t see …

A state-of-the-art C++11 solution would be perfect 🙂

My thoughts so far:
Solution 1 seems OK, but is C++11 only and I would have to create the returned object on the heap in order to be able to pass it to the main program (returning the object itself to the calling function, thus keeping it in the stack might be quicker for small objects?).
Solution 2 might be slower and leads to verbose coding in the main program.
Solution 3 is probably the slowest (an object is created in vain) and not very convenient to check for in the main program.

For my code no valid return object is rather the default situation than the exception and the created object is rather small, but general considerations considering different cases are certainly useful for other readers’ applications …

Thanks a lot to all of you for help 🙂

  • 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-13T14:49:12+00:00Added an answer on June 13, 2026 at 2:49 pm

    All 3 of your suggested solutions are valid and common, depending on the circumstances.

    If being unable to create the object is an error condition that is likely to cause the calling function to have to abort, back up and retry, or take other drastic measures, then throw an exception.

    If inability to create the object is a routine event, and you expect the caller to check if an object was created and proceed fairly normally in either case, returning null is a good solution.

    If there’s a reasonable dummy or blank object that can be created, that’s a fine solution. But this is pretty rare. You should only do this if the caller will actually process the dummy object.

    If you return a null pointer and then you find that every place you call this function you are writing

    MyObject* myobject=myfunction(whatever);
    if (myobject==null) throw new PanicException;
    

    Then you might as well just throw the exception inside the function.

    Worse, if you are writing:

    MyObject* myobject=myfunction(whatever);
    if (myobject!=null)
    {
      ... process it ...
    }
    else
    {
       ... display error message ...
    }
    

    Then you are just simulating exception handling with an IF statement. Use a real exception.

    On the other hand, if you throw an exception and then you find you are regularly writing:

    MyObject* myobject;
    try
    {
      myobject=myfunction(whatever);
    }
    catch (PanicException pe)
    {
      myobject=null;
    }
    

    Well then, you would have been better off to just return the null.

    I’ve occasionally created dummy objects. The most common case is when a function returns a collection, like an array or linked list, and if I find no data to put in the collection, then return a collection with zero elements. Then the caller loops through the elements in the collection, and if there are none, that’s just fine. I’ve had a few cases where I’ve returned an object with a zero-length string for the name or customer id or whatever. But in general, if you’re just returning a dummy object so that the caller can test and say, oh, it’s a dummy object, and then throw it away, I think you’re better off to return null.

    BTW not sure what you meant when you said that you could only return a null pointer in C++11. The ability to pass around nulls goes back to the earliest version of C++ that I ever saw.

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

Sidebar

Related Questions

I currently have a function: public static Attribute GetAttribute(MemberInfo Member, Type AttributeType) { Object[]
How can I get the return type of a member function in the following
Possible Duplicate: CakePHP: Call to a member function find() on a non-object I have
Say I have a type with a member function: class Thing { std::string m_name;
In F# I have a function that returns System.Linq.Expression instances: and System.Object with member
I have got this error No default member found for type 'VB$AnonymousDelegate_0(Of SqlDataReader,String,Object)'. My
Say I have some sort of type wrapping up a function, maybe a lambda
I have a member function in a class that has a callback, but the
I have an inline member function defined under class MyClass int MyClass::myInlineFunction(); This function
I wonder if it is a good practice to have a member template function

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.