exit in cpp


 
Thread Tools Search this Thread
Top Forums Programming exit in cpp
# 1  
Old 08-18-2009
exit in cpp

In a program if we call exit(0), it exits the program and before that it closes all opened stream.

In C++, it even does destroys the created objects. Is there any function available, which if called will do some basic clean ups (which includes object destruction) ???
# 2  
Old 08-18-2009
Sounds like you are looking for the atexit() function.
# 3  
Old 08-18-2009
yes....but atexit() does not take any arguments. How can this be implemented in object oriented paradigm ???
# 4  
Old 08-18-2009
Quote:
Originally Posted by lipun4u
yes....but atexit() does not take any arguments.
atexit() does take arguments as it calls a function when exiting the program, however the called function cannot return arguments. Have a look at this page.

If you want your program to return a value, you could try the _exit() function. More info here.
# 5  
Old 08-18-2009
Throw an exception.

What you want to do is throw an exception.

Code:
// Special exception to throw
class exit_exception : public runtime_error
{
  public:
     exit_exception() : runtime_error("Termination"), _code(0) {}
     exit_exception(int code) : runtime_error("Termination"), _code(code){}
     virtual ~exit_exception(){}
     int getStatusCode()const{ return _code; }
  private:
      int _code;
};

Code:
// Main function
int main(int argc, char* argv[])
{
    int retcode = 0;
    try {
          // program content...
    }catch(const exit_exception& quit){
         retcode = quit.getStatusCode();
    }
    return retcode;
}

Code:
// ...
throw(exit_exception(0)); // Instead of exit(0)
// ...

Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Programming

.h or .cpp

I have the code below and cannot decide if to put it in a .h file or in a .cpp file #ifndef VERBOSE_H #define VERBOSE_H #include "sstring.h" enum Verbose { none = 0, low = 1, medium = 2, high = 3, diag = 4 }; bool GetVerbose(String& S, Verbose& V) { S.ToUpper(); if (S ==... (3 Replies)
Discussion started by: kristinu
3 Replies

2. Shell Programming and Scripting

CPP to PERL

Hi all, can we convert a cpp program to perl scripting ? (4 Replies)
Discussion started by: Shell_Learner
4 Replies

3. Shell Programming and Scripting

reading a cpp file

I need to find all the methods in a cpp file ... using shell script Pls guide me regarding the grep criteria for searching methods I mean what are the patterns to be grepped in *.cpp which match methods Hope i have made myself clear Thanks and Regards -- Ultimatix (2 Replies)
Discussion started by: ultimatix
2 Replies

4. Shell Programming and Scripting

If file = .cpp then print?

I'm trying to develop a script that makes it so only .cpp programs can print. I'm doing it for my computer programming class because everyone keeps printing the executable instead of the source code and it's wasting a lot of paper. How can I accomplish this? Thanks for the help. :D (5 Replies)
Discussion started by: Irish_Cereal
5 Replies

5. Programming

gcccommand found how to do with cpp

gcc help iam using kubuntu os (www.ubuntu.com) in that i dont find gcc but cpp command is there how to compile code with that & how to use gij for java in ubuntu (1 Reply)
Discussion started by: seshumohan
1 Replies

6. UNIX for Dummies Questions & Answers

Where can I find a list of exit codes? (Exit code 64)

I'm receiving an exit code 64 in our batch scheduler (BMC product control-m) executing a PERL script on UX-HP. Can you tell me where I can find a list of exit codes and their meaning. I'm assuming the exit code is from the Unix operating system not PERL. (3 Replies)
Discussion started by: jkuchar747
3 Replies

7. Programming

cpp in unix

sir i am trying to compile and execute cpp file in unix the command cpp <filename > is not working do you suggest any other command? thanking you (5 Replies)
Discussion started by: sandhyapidugu
5 Replies

8. UNIX for Dummies Questions & Answers

Hi Modem Problem And cpp

Hi am very new to unix.I got installed linux mandrake and the first problem is whith the modem i don't know why but it does not work. i config it on /dev/modem and some time it says "the modem is bussy" and some time it says: "Modem ready" but the modem did'n switch on Ok the other question... (3 Replies)
Discussion started by: user666
3 Replies
Login or Register to Ask a Question