I have a class
class ChartLine{
protected:
vector<Point> line; // points connecting the line
CString name; //line name for legend
CPen pen; //color, size and style properties of the line
};
where Point is a structure
struct Point{
CString x;
double y;
};
In main() I dynamically allocate objects of type ChartLine with new operator.
If I use delete afterwards, will default destructor ~ChartLine() properly dealocate (or clear) member ChartLine::line(which is vector btw) or I would have to clear that vector in ~ChartLine() manually?
Thanks in advance.
Cheers.
The implicitly created destructor will call the destructor of all the members (in the reverse order they are declared in the class.) The
vectorwill clean up after itself. You don’t need to define a destructor yourself.This is why you should prefer automatic allocation in combination with RAII. When objects clean themselves, your code as safer and easier. Hint: Don’t use new and delete, put it in a smart pointer!
Both of those will delete automatically, and now you’re exception safe as well. (Note, the two above do not do the same thing. There are more types of smart pointers as well.)