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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:33:50+00:00 2026-06-08T11:33:50+00:00

This may seem like an odd question. I’m currently moving certain objects from a

  • 0

This may seem like an odd question. I’m currently moving certain objects from a two-step to a one-step initialization scheme. Basically moving what was done in the .initialize() .terminate() member functions into the constructors and destructors.

My issue is that, it is important to know whether or not these classes initialized certain attributes correctly that depend on external factors.

An example is my Window class which creates a WinAPI window. Previously using the two step method I would have initialize() return a boolean of whether or not the window was created properly.

if(myWindow.initialize())
{
    // proceed with application
}
else
{
    // exit
}    

Is there anyway to relay this information from the constructor without creating and having to call a second method, such as didMyWindowInitializeCorrectly()?

At first I was hoping something along the lines of

if(Window *myWindow = new Window)
{
    // proceed with application
}
else
{
    // exit
}

But this won’t work because the Window object will still instantiate even though the window creation failed.

Is the only solution to have the constructor throw an exception and then catch it and proceed? I’ve looked at a lot of threads and people’s opinions of C++ exceptions seems pretty split so I’m unsure of what is the best approach.

Is there anyway to handle this situation with an if statement?

  • 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-08T11:33:51+00:00Added an answer on June 8, 2026 at 11:33 am

    This comes down to the big argument of Exception or error code based initialization. I usually prefer Exceptions for indicating constructor failure as the problem becomes more apparent earlier in a stack trace when the problem isn’t handled over is_valid checks later in code or ignoring two-step return values. It’s also clearer about why initialization failed without needing error code lookups. People who prefer the conventional C style over Exception style failure messaging will probably disagree with me on that last point.

    However, it’s oftentimes a good idea to match the style of the rest of the code base. So if Exceptions aren’t used anywhere else in the code, it’s probably better to have a is_valid check (or the original two stage initialization) over introducing a mechanism for object validation. I prefer the is_valid over two stage much like Riateche posted, as it leaves the user capable of choosing if/when they want to check it’s valid without needing for them to go lookup what functions have to be called before they should legally use the object.

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

Sidebar

Related Questions

This may seem like an odd question but I was talking to a friend
This may seem like an odd question, but I need to find a closed-source
This may seem like an odd question, but I have my own reasons for
This may seem like an odd one, but I want to be able to
This may at first seem like an odd question, but when a cin request
This may seem like an odd question, but does anyone know the origin of
This may seem like an odd question, but it's something I couldn't solve on
This may seem like a stupid Question but i have two cubes being rendered
This may seem like an odd question, but I'm looking for a comment generator
This may seem like an absurd question, however thought I best double check beforehand.

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.