|
-
Nov 7th, 2002, 01:04 PM
#1
Thread Starter
Stuck in the 80s
[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.
-
Nov 7th, 2002, 01:19 PM
#2
Monday Morning Lunatic
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
-
Nov 7th, 2002, 02:00 PM
#3
Thread Starter
Stuck in the 80s
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."
-
Nov 7th, 2002, 02:04 PM
#4
Monday Morning Lunatic
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
-
Nov 7th, 2002, 02:09 PM
#5
Thread Starter
Stuck in the 80s
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.
-
Nov 7th, 2002, 02:16 PM
#6
Monday Morning Lunatic
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
-
Nov 7th, 2002, 02:16 PM
#7
Thread Starter
Stuck in the 80s
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.
-
Nov 7th, 2002, 02:20 PM
#8
Monday Morning Lunatic
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
-
Nov 7th, 2002, 02:29 PM
#9
Thread Starter
Stuck in the 80s
-
Nov 7th, 2002, 02:33 PM
#10
Frenzied Member
My destructors usually look like this:
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.
-
Nov 7th, 2002, 02:35 PM
#11
Thread Starter
Stuck in the 80s
anywhere I can find a little more information that Release()?
-
Nov 7th, 2002, 02:37 PM
#12
Thread Starter
Stuck in the 80s
Where is that at? I tried including stdlib, but I get:
C:\Programming\Cpp Stuff\classcrap\classcrap.cpp(28) : error C2065: 'Release' : undeclared identifier
-
Nov 7th, 2002, 02:38 PM
#13
Monday Morning Lunatic
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
-
Nov 7th, 2002, 02:39 PM
#14
Frenzied Member
Release() is just another member function that you define... Look at my full class definition.
Z.
-
Nov 7th, 2002, 02:41 PM
#15
Frenzied Member
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.
-
Nov 7th, 2002, 02:42 PM
#16
Thread Starter
Stuck in the 80s
Oh. I see.
I thought it was a magical function or something.
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
|