Help with destructor

This is a discussion on Help with destructor within the C++ Programming forums, part of the General Programming Boards category; In the following program Code: #include<iostream> using namespace std; void MyExit(void) { cout<<endl<<"Exit from function!"<<endl; exit(0); } class MyClass { ...

  1. #1
    Infant of C
    Join Date
    May 2010
    Location
    Karachi, Pakistan
    Posts
    118

    Help with destructor

    In the following program

    Code:
    #include<iostream>
    using namespace std;
    void MyExit(void)
    {
    	cout<<endl<<"Exit from function!"<<endl;
    	exit(0);
    }
    class MyClass
    {
    	public:
    	MyClass(void)
    	{
    		cout<<endl<<"Object initialized!";
    	}
    	~MyClass()
    	{
    		cout<<endl<<"Object destoryed!"<<endl;
    	}
    };
    int main()
    {
    	MyClass C1;
    	MyExit();
    	return 0;
    }
    The destructor is not executing. What is the reason?
    Although I am not terminating the program in main() but still on termination C! should go out of scope which means that the dertructor should run. But it is not happening. Why?

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    You're terminating the program before the variable's scope ends. The scope lasts until main returns.
    If you wanted it to be destroyed earlier then you'd need to insert an additional scope:
    Code:
    int main()
    {
    	{
    		MyClass C1;
    	}
    	MyExit();
    	return 0;
    }
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,620
    To be more specific, calling exit() "terminates the program without leaving the current block and hence without destroying objects of automatic storage duration" (that is a direct quote from the C++ standard, Section 3.6.1, para 4). C1 is an object of automatic storage duration. For struct/class types, the destructor is invoked during the process of destroying an object.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,001
    Avoid using exit. In C, it might be justified, but in C++, as you see, it can cause all sorts of problems.
    Always let main go out of scope.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Infant of C
    Join Date
    May 2010
    Location
    Karachi, Pakistan
    Posts
    118
    Well if I want to run destructor at every point I terminate the program. What should I do. I may be using this exit() at more than one point.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,001
    Don't use exit. Use exceptions instead to signal an error and (possibly) catch it in main.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Infant of C
    Join Date
    May 2010
    Location
    Karachi, Pakistan
    Posts
    118
    Well I will post my actual program were I need to do this.

  8. #8
    Infant of C
    Join Date
    May 2010
    Location
    Karachi, Pakistan
    Posts
    118
    very soon

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,620
    Read Elysia's last post again. Apart the advice not to call exit(), there is also a suggestion of what you can do instead. No need to post code where you consider you need to call exit().
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to use a destructor
    By Noobie in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2003, 12:40 AM
  2. destructor and how to use it
    By Micko in forum C Programming
    Replies: 1
    Last Post: 11-22-2003, 12:50 PM
  3. destructor
    By bharathbangera in forum C++ Programming
    Replies: 5
    Last Post: 05-18-2003, 01:31 PM
  4. Destructor help
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 05-27-2002, 05:58 AM
  5. destructor (any need)
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 01-24-2002, 10:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21