Original Post
I am having problems trying to figure out how to get my operator+ to work. I believe the problem is down to the fact that p is a pointer, but i cant for the life of me figure out how to solve it. I would be very grateful if someone would point me in the right direction. (as soon as i add (temp = a) to my + operator, my Point array messes up.) Thanks, Dan
class Obstacle{
Point *p;
int n;
public:
Obstacle();
Obstacle(int);
~Obstacle();
Obstacle &operator+= (const Obstacle&);
Obstacle &operator= (const Obstacle&);
Obstacle &operator+ (Obstacle);
};
Obstacle &Obstacle::operator+= (const Obstacle &a)
{
Point *temp = new Point[n];
for (int i = 0; i < n; i++) temp = p;
if (p != NULL) delete[] p;
p = new Point[n + a.n];
for(int i = 0; i < n; i++) p = temp;
for(int i = 0; i < a.n; i++)p[n+i] = a.p;
n+=a.n;
if (temp != NULL) delete[] temp;
return *this;
}
// overloaded = operator
Obstacle &Obstacle::operator= (const Obstacle &a)
{
if(this != &a){
if (p != NULL) delete[]p;
p = new Point[a.n];
for (int i = 0; i < a.n; i++)p = a.p;
n = a.n;
}
return *this;
}
Obstacle &Obstacle::operator+ (Obstacle a)
{
Obstacle temp = Obstacle();
temp = a;
return temp;
}