Results 1 to 16 of 16

Thread: [Resolved] Classes and Destructors

  1. #1

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    [Resolved] Classes and Destructors

    I've never officially learned about classes, I just use them all the time but never really understand them. So a few questions:

    1) What is the purpose of the desctructor? Does it just clear up memory?

    2) What's wrong with this?:

    Code:
    class square_info {
        private:
            float h, w;
    
    
        public:
            square_info () {
                h = 0;
                w = 0;
            }
    
            square_info (float height, float width) {
                h = height;
                w = width;
            }
    
            void print() {
                cout << "h: " << h << " | w: " << w << endl;
            }
    
            ~square_info(); //adding this line gives linking errors?
    };
    Thanks in advance.
    Last edited by The Hobo; Nov 7th, 2002 at 02:30 PM.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    It gives linker errors because you haven't defined any code for it.

    A destructor isn't always required, no. If all the members in your class can automatically clear themselves up (like a string, int, whatever), you're ok. If you start storing pointers, then the destructor would need to deallocate that memory, or maybe release a mutex lock, or something.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    What code should be defined for it?

    And for now, I have to use it in my code because that's one of the requirements for my class assignments "have a constructor and destructor."
    My evil laugh has a squeak in it.

    kristopherwilson.com

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You do not need a destructor unless you require additional functionality. The compiler takes care of freeing the memory used by the instance itself, and calling the destructors of your members.

    In this case, your destructor would just be empty. I'd remove the destructor and put a note saying that none was required.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Okay.

    I'll just comment it out and put a little note in the code and hope she doesn't mark me down for it.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    If she does...

    a) complain loudly

    b) send her my email address and tell her to explain herself

    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Just for reference, then, what type of code would go in the destructor?

    Code:
    ~class_name() {
      pvariable = NULL;
    }
    I'm just trying to understand the destructor.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Say you had some kind of buffer class:
    Code:
    template <typename T>
    class buffer {
    public:
        buffer(size_t sz) {
            m_buf = new T[sz];
        }
    
        virtual ~buffer() {
            delete[] m_buf;
        }
    
        T& operator[](int idx) {
            return m_buf[idx];
        }
    
    private:
        T *m_buf;
    };
    Can you see what's happening here? The virtual on the destructor makes sure that m_buf is deleted even if you have a derived class.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  9. #9

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Thanks.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  10. #10
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    My destructors usually look like this:
    Code:
    ~x(){
        Release();
    }
    The Release() function essentiall cleans everything up, and resets the object to an uninitialized state, ie:
    Code:
    class Cup // yeah, whatever, there is a cup on my desk...
    {
    private:
        float m_amt;
    public:
        Cup(){m_amt = 0.0f;}
        ~Cup(){ Release(); }
        void Fill(){ m_amt = 1.0f; }
        void Empty() {m_amt = 0.0f; }
        int Release() { m_amt = 0.0f; }
    };
    Z.

  11. #11

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    anywhere I can find a little more information that Release()?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  12. #12

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Where is that at? I tried including stdlib, but I get:

    C:\Programming\Cpp Stuff\classcrap\classcrap.cpp(28) : error C2065: 'Release' : undeclared identifier
    My evil laugh has a squeak in it.

    kristopherwilson.com

  13. #13
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    It's one of his, he defines it himself.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  14. #14
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Release() is just another member function that you define... Look at my full class definition.

    Z.

  15. #15
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    The basic idea is that you can call Release() to reset the object at any point in your code, without actually destroying the object in question. You CAN do this with the destructor(calling it), though if you have a nice list of derived objects, its a bit easier to just call ->Release() on all of them.

    Z.

  16. #16

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Oh. I see.

    I thought it was a magical function or something.
    My evil laugh has a squeak in it.

    kristopherwilson.com

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