C
cppaddict
Hi,
I am deleting some objects created by new in my class destructor, and
it is causing my application to error at runtime. The code below
compiles ok, and also runs fine if I remove the body of the
destructor. So I think I am somehow using delete incorrectly, but I'm
not sure exaclty what I'm doing wrong.
I'd apprecitate any clarification and suggestions for rewriting the
below properly.
Thanks,
cpp
PS: The Char class below is used to store a pixel representation of a
character for display. A character in this context is simply a
collection of points.
#include <vector>
#include "Point.h"
class Char {
private:
char _ch;
std::vector<Point> _pixelPoints;
public:
Char(char);
~Char();
void addPoint(int, int);
std::vector<Point>& const getPixelPoints();
char getChar() {return _ch;}
};
Char::Char(char ch) :
_ch(ch)
{ }
Char::~Char() {
std::vector<Point>::iterator iter;
for (iter=_pixelPoints.begin(); iter!=_pixelPoints.end();
iter++)
delete iter;
}
void Char::addPoint(int x, int y) {
_pixelPoints.push_back(*( new Point(x,y) ));
}
std::vector<Point>& const Char::getPixelPoints() {
return _pixelPoints;
}
I am deleting some objects created by new in my class destructor, and
it is causing my application to error at runtime. The code below
compiles ok, and also runs fine if I remove the body of the
destructor. So I think I am somehow using delete incorrectly, but I'm
not sure exaclty what I'm doing wrong.
I'd apprecitate any clarification and suggestions for rewriting the
below properly.
Thanks,
cpp
PS: The Char class below is used to store a pixel representation of a
character for display. A character in this context is simply a
collection of points.
#include <vector>
#include "Point.h"
class Char {
private:
char _ch;
std::vector<Point> _pixelPoints;
public:
Char(char);
~Char();
void addPoint(int, int);
std::vector<Point>& const getPixelPoints();
char getChar() {return _ch;}
};
Char::Char(char ch) :
_ch(ch)
{ }
Char::~Char() {
std::vector<Point>::iterator iter;
for (iter=_pixelPoints.begin(); iter!=_pixelPoints.end();
iter++)
delete iter;
}
void Char::addPoint(int x, int y) {
_pixelPoints.push_back(*( new Point(x,y) ));
}
std::vector<Point>& const Char::getPixelPoints() {
return _pixelPoints;
}