CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2008
    Posts
    4

    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>??

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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>.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Apr 2008
    Posts
    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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).
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: How come cout works in destructor function?

    Quote 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).

  6. #6
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    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

  7. #7
    Join Date
    Apr 2008
    Posts
    4

    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
  •  





Click Here to Expand Forum to Full Width

Featured