Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Ending a program from within the constructor

Posted on 2004-03-27
7
Medium Priority
?
245 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

722 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