08-18-2005, 03:33 PM
Say I have a class:
header file:
definition file:
At position 1, I want to assign to the private "name" field the value passed in, which is a string.
Currently I am doing it by:
Is this correct? Will it result in memory leaks?
And at position 2, I can't seem to get the memory for it freed correctly. If I do:
It complains that I didn't pass a pointer to delete. Any other way I try I get a segfault...
So what should be done in the destructor?
header file:
Code:
class Widget {
public:
Widget(string);
~Widget();
private:
string name;
};
definition file:
Code:
Widget::Widget(string name) {
// 1
}
Widget::~Widget() {
// 2
}
At position 1, I want to assign to the private "name" field the value passed in, which is a string.
Currently I am doing it by:
Code:
this->name = name
Is this correct? Will it result in memory leaks?
And at position 2, I can't seem to get the memory for it freed correctly. If I do:
Code:
delete [] name;
It complains that I didn't pass a pointer to delete. Any other way I try I get a segfault...
So what should be done in the destructor?