|
-
April 13th, 2008, 07:07 AM
#1
How come cout works in destructor function?
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>??
-
April 13th, 2008, 07:14 AM
#2
Re: How come cout works in destructor function?
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>.
-
April 13th, 2008, 07:19 AM
#3
Re: How come cout works in destructor function?
My doubt is if the destructor is run after the exit from the main method. Who controls it? Like how is cout executed.
-
April 13th, 2008, 07:35 AM
#4
Re: How come cout works in destructor function?
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).
-
April 13th, 2008, 07:38 AM
#5
Re: How come cout works in destructor function?
 Originally Posted by Mohantek
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).
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
April 13th, 2008, 11:41 AM
#6
Re: How come cout works in destructor function?
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 .....)
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
May 22nd, 2009, 08:49 AM
#7
Re: How come cout works in destructor function?
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...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|