Solved

Ending a program from within the constructor

Posted on 2004-03-27
7
217 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 50 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

805 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