class Point {
private:
int x;
int y;
public:
Point();
void setX(int);
void setY(int);
int getX();
int getY();
};
Point::Point() {
x = 0;
y = 0;
}
void Point::setX(int value) {
x = value;
}
void Point::setY(int value) {
y = value;
}
int Point::getX() {
return x;
}
int Point::getY() {
return y;
}
which is in another class
1 2 3 4 5 6 7 8 9 10
class EntityBase {
private:
Point position;
public:
Point getPos();
};
Point EntityBase::getPos() {
return position;
}
and that class has a derived class
1 2
class Player : public EntityBase {
}p1;
When I try to do p1.getPos().setX(10), it does not do anything. In debug mode x and y of position always stay at 0. Am I doing something wrong? Should I have a setX() function in EntityBase?
You need to have your getPos() function either return a reference to your point (which is not recommended since it would kind of defeat the point of having position private), or Create a setX() function in EntityBase that just calls position's setX().