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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:40:26+00:00 2026-05-20T14:40:26+00:00

I have a create method as shown below, in which I want to (if

  • 0

I have a create method as shown below, in which I want to (if the return is a null pointer) get the error from. However I want this error handling to be optional, and not a requirement in calling the function.

The code:

class foo {
    foo() {...};
public:
    ~foo();
    static foo* createFoo(int aparam = 42, int bparam = 10, int& error = ?) {
        afoo* = new foo();
        if (!afoo) {
            error = 11;
            return 0; //NULL
        }
        ...
        return afoo;
    }

}

So I can then decide to either use:

foo* myfoo = createFoo(42);
if (!myfoo) { ... }

Or

int errorcode = 0;
foo* myfoo = createFoo(42, 10, errorcode);
...

At this point (in my real code), I am just using a null pointer (instead of a ref) and determining its validity in the createFoo code before giving it error.

My interest is in the best practice for this situation.

  • 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-20T14:40:26+00:00Added an answer on May 20, 2026 at 2:40 pm

    I don’t know that I can offer best practice but here is my perspective.

    Checking for NULL (a failed allocation) is standard practice in C and C++, it is a well understood and easily recognized idiom. The addition of an optional error code, while providing flexibility also adds arguably unneeded complexity. It burdens the caller to decide whether they will use the parameter.

    Why would they use the parameter unless they felt it was likely to fail in that location? How would

    If you feel the error code is significant. I would suggest the alternate signature, whereby you always check the return code, and do not check the value of the returned pointer:

    // Returns error code.
    static int createFoo(int, int, Foo **);
    

    This is a perhaps less convenient function, but nudges the caller (user) in the right direction.

    Alternately, you could use exceptions, either ensuring std::bad_alloc is thrown, or throwing an exception of your own creation with the appropriate error code. This would seem to be the cleanest signature:

    struct fooException { int error; }
    
    // Throws fooException if cannot create foo.
    static foo * createFoo(int, int);
    

    The philosophy I am driving at is: minimize complexity. In this case by removing what seems to be an extraneous option. Either the error code is significant and should always be used, or it extraneous and will always be ignored.

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

Sidebar

Related Questions

I have a method that's about ten lines of code. I want to create
I would like to create a safe sum extension method that would have the
I have created a .NET DLL which makes some methods COM visible. One method
I have a method that creates a MessageDigest (a hash) from a file, and
I want to create a program which calculates how long it will take to
I have implemented the two classes shown at http://tomcat.apache.org/tomcat-6.0-doc/aio.html which gives a messenger application
Given a DataSet, I'm left joining DataTables[1-n] onto DataTable[0]. I have created a method
I have created a console application that calls a method on a webservice. I
I have a method running in a seperate thread. The thread is created and
Suppose I have BaseClass with public methods A and B, and I create DerivedClass

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.