Thread: Simple class question

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    13

    Simple class question

    Realllllly simple question -> if you create a class the constructor is called. Eventually (unless the program ran forever), when it quits (or similar) the deconstructor would have to be called, right?

    The reason I asked is because I wrote a very simple DLL for use in a financial software package - it works perfectly, except for 1 problem - i can log (in an output file) anytime a class is created, but classes are never EVER deconstructed (i know because in the deconstructor i have it logging to an output file, and there's never any reference to the deconstructor).

    I just can't figure out if that's normal (deconstructor not being called after a DLL is finished using if the DLL creates a class) or not

  2. #2
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    No, a constructor is not called when you create a class. It's created when you create an OBJECT. And a destructor is called whenever you DESTROY that object. Whether you use the delete keyword to delete an object, or the program ends, the object will be destroyed, and the destructor will be executed.

    Code:
    #include <iostream>
    using namespace std;
    
    class Doggy
    {
    public:
      Doggy(){cout<<"Object created." <<endl;}
      ~Doggy(){cout<<"Objected destroyed."<<endl;}
    };
    
    int main(void){
       Doggy *Max = new Doggy;
       delete Max;
       cin.get();
       return 0;
    }
    So if you never create an object of your class, you can't destroy it can you? Neither the constructor, not the destructor will be executed. Just having the class there won't call either of them.
    Last edited by Krak; 04-20-2005 at 08:59 PM.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    13
    yea, my bad, i typed that in a hurry since i was leaving and wasn't careful with my wording. let me re-express it.

    my DLL, when called from the software package (TradeStation) will create an object. The constructor is definately called because it is logged in a separate output file. So it's definately been created.

    However, even the software finishes using the DLL, even after I close the application completely, the descructor has never been called because there's no output.

    So if the constructor is called showing the object was created, eventually the deconstructor has to be called, right? (unless it just sits in memory until you turn off the computer)

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Just a footnote to Krak's brilliant answer to emphasize this:

    deconstructor------->destructor

    Learn it, live by it.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    So if the constructor is called showing the object was created, eventually the deconstructor has to be called, right? (unless it just sits in memory until you turn off the computer)
    As Krak explained:

    Whether you use the delete keyword to delete an object, or the program ends, the object will be destroyed, and the destructor will be executed.
    When my programs end, I don't get any file output either, but that may have something to do with the fact I haven't programmed my destructors to do that.

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    13
    Quote Originally Posted by 7stud
    As Krak explained:


    When my programs end, I don't get any file output either, but that may have something to do with the fact I haven't programmed my destructors to do that.

    right, except i've programmed mine to give me output and i never get any feedback from the destructor, so therefor it makes me wonder if it's being called......

  7. #7
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Post code maybe?
    That ussually helps the debugging process
    Woop?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 01-13-2008, 05:57 PM
  2. class and pointer question
    By l2u in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2006, 10:00 AM
  3. Simple thread object model (my first post)
    By Codeplug in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2004, 11:34 PM
  4. Yet another simple class question...
    By Ricochet in forum C++ Programming
    Replies: 3
    Last Post: 12-10-2003, 09:06 PM
  5. Simple Question, I think
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 04-01-2002, 10:36 AM