Click to See Complete Forum and Search --> : How come cout works in destructor function?
Mohantek
April 13th, 2008, 07:07 AM
How come destructor execute the below code? When the destructor is invoked after exit from main method??
~complesx()
{
cout << "How is it possible"
}
When I run my next program w/o clear screen, I can see this previous o/p "How is it possible"
My questions are?
If destructor is invoked after exit from main method, how is it possible to execute the print statement when there is no prototyping available?
From where it includes the file <stdio.h> or <conio.h>??
laserlight
April 13th, 2008, 07:14 AM
How come destructor execute the below code?
If code in the body of a destructor is never run, it implies that destructors have no purpose. Their bodies would be for fun, and writing them would be a total waste of time.
When the destructor is invoked after exit from main method?
The destructor is invoked when the object goes out of scope.
If destructor is invoked after exit from main method, how is it possible to execute the print statement when there is no prototyping available?
From where it includes the file <stdio.h> or <conio.h>??
What do you mean? std::cout comes from <iostream>.
Mohantek
April 13th, 2008, 07:19 AM
My doubt is if the destructor is run after the exit from the main method. Who controls it? Like how is cout executed.
laserlight
April 13th, 2008, 07:35 AM
My doubt is if the destructor is run after the exit from the main method. Who controls it? Like how is cout executed.
You can consider the destructor as being run just before exit from the main method (or any scope where an object is destroyed).
exterminator
April 13th, 2008, 07:38 AM
My doubt is if the destructor is run after the exit from the main method. Who controls it? Like how is cout executed.cout is a global object that is created before code in main starts executing. Your objects are created inside main (or may be before it if they are globals). But if these objects use cout, cout should get created before your objects do or atleast before your objects start using it.
The order of destruction is typically the reverse of the order of construction. And the destructor call is pretty much by magic in C++. You don't call destructors yourself (well, usually and atleast not in the case above).
TheCPUWizard
April 13th, 2008, 11:41 AM
It is completely WRONG to thing that your program "starts and ends" with "main()" there are many things that run both before and after.
Not understanding this shows a major lack in the most basic concepts of programming in C++ (or C, or C# or VB or .....)
Mohantek
May 22nd, 2009, 08:49 AM
Something are basic to someone and advanced to some people. Not knowing the basic stuff always points to taht you are eager to learn.
Sorry for the preaching but...
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.