Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Ending a program from within the constructor

Posted on 2004-03-27
7
Medium Priority
?
250 Views
Last Modified: 2010-04-01
I'm pretty new to C++ so this may prove to be a simple question.  I have class hierarchy that is used to illustrate PC's and their components, with the base class I'm working with being the PC.  I have done some validation in the construtor using  a dynamic_cast to down cast the CPU entered by the user to a derived class.  If the cast fails then the PC can not be fitted to this kind of PC.  This all works fine but I would really like to end the program there and then from within the constructor.  At the moment all I do is cout a message saying there has been an error.  Is it possible to do something in here? I tried immediatly calling the destructor from within the constructor but that failed.

Any help at all will be a god-send.

many thanks

Janusz
0
Comment
Question by:jstabik
  • 2
7 Comments
 
LVL 44

Accepted Solution

by:
Karl Heinz Kremer earned 200 total points
ID: 10694490
You could just call exit()
0
 
LVL 5

Expert Comment

by:info_expert
ID: 10695186
Ok ,
Call the function:
exit(0);

it will exit the program.
0
 
LVL 5

Expert Comment

by:info_expert
ID: 10695196
One more thing.
You can not call destructor within the constructor.
0
 
LVL 1

Expert Comment

by:GJanusz
ID: 10695247
Normally, if you want to do anything non-trivial in a constructor, it is best to change the constructor to be very simple (perhaps empty), and create an "Initialize()" class member function that you always call after constructing an instance of the class.  The most common reason to so this that I have run into is the situation you are in: you want to handle an error condition.

This would allow you to exit the program in a more graceful and structured manner (traversing up the call stack on an error condition).  Then you'd end up back in main() and you can exit() with an error code.  For example:

main()
{
      int nExitCode;
      nExitCode = MyFunc();
      if (nExitCode == 0) {
            // other function calls when MyFunc was successful
      }
      exit(nExitCode);
}

// Returns 0 on success
int MyFunc()
{
      int nReturnVal;
      MyClass x;
      nReturnVal = x.Initialize();
      if (nReturnVal != 0) {
            return nReturnVal;
      }
      // Other code executed when x initializes OK
}

It doesn't sound like you are using VC++ and MFC, but just in case, I'll mention that the above suggestion is a good way to do it so that an AfxGetMainWnd()->PostMessage(WM_CLOSE) would shut down the program in the normal Windows way (i.e., you get back into the Windows message handling loop).
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question