Solved

Ending a program from within the constructor

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now