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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:11:57+00:00 2026-05-25T19:11:57+00:00

I am working on refactoring some of the old C-style code to bring it

  • 0

I am working on refactoring some of the old C-style code to bring it more into line with C++ code. I still am a bit new to C++

An example of the code I am working on is as follows

Errmsg foo{
   ErrMsg err = NoError;
   /*
    Some Processing
   */
  err = foo_cleanup(err,/* some parameters*/);
   /*
    Some More Processing
   */
  return err;
}

I was thinking of developing a class so that

class foo_class
{
 public:
   foo_class(Errmsg errmsg_init&,/*Some other arguments */ ):
      errmsg(&errmsg_init),
      /*Initialize other parameters */{}

   void foo_cleanup (/*Other parameters*/);
   // same functionality, but since the source variable is available, 
   // it can be modified without having to return any variable

  ~foo_class(){foo_cleanup(/*Parameters*/);}

   /*
   Member functions
   */

 private:
   Errmsg* errmsg;
   /*Some other parameters */
};

Errmsg foo{
   ErrMsg err = NoError; 
   foo_class foo_obj(err);
   /*
    Some Processing
   */

  // The class would be 
  //cleaned up before returning 
  // and the err variable would be 
  //modified in the destructor
  return err;
}

Although I have been able to use something similar to this approach, I do not know if this will be portable.

Is this the right thing to do?

If not, do I just use pointers to initialize the class instead of passing the error message variable by reference? Or is there something else I can do?

I cannot use exceptions at the current stage because there are many function calls to/from external code which use a “return error message” approach still.

  • 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-25T19:11:57+00:00Added an answer on May 25, 2026 at 7:11 pm

    Leaving aside that you should if possible fix the calling code so that an exception is OK, you could use the two-phase construction idiom:

    struct foo_class {
        foo_class() { } // default constructor cannot fail
        Errmsg nothrow_init(/* other params */) throw() {
            /* initialize other parameters */
        }
        foo_class(/* other params */) {
            if (nothrow_init(/* other params */) != ERRMSG_OK) {
                throw something;
            }
        }
    };
    

    Now, exception-using code can call the multi-arg constructor (and get the object in a ready-to-use state), whereas exception-averse code can call the default constructor followed by nothrow_init (and live with the fact that if nothrow_init fails they have an unusable object on their hands, and it’s their responsibility to ensure that they don’t use it).

    In future, as you bring other parts of your code base into an exception-using state, you will find that the code in init calls things that can themselves throw. At that point you can start moving code around, so that the nothrow_init calls an init function that can throw, but catches any exceptions and turns them into error codes. Assuming it’s an internal class then eventually nothing will be using nothrow_init and you can remove it.

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

Sidebar

Related Questions

I'm refactoring some code in a project I'm working on and I ran into
I'm working on refactoring a suite of old utilities and creating a new server
I am working on Solaris 10, Sun Studio 11. I am refactoring some old
I'm refactoring some code in C++, and I want to deprecate some old methods.
Some time I ago I was working on a major refactoring of an old
I'm working on some refactoring of legacy code to move styles to a stylesheet
I am working on some OLD (as in older than me) C code that
I'm refactoring some code so that my app will pull data from a website
I tried to do some refactoring to convert an each block into an inject,
What are some common strategies for refactoring large state-only objects? I am working on

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.