Classes in Classes

I am having trouble getting a function to work in Visual C++. I have a Point class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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?
Last edited on
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().
Thank you. That seems to have worked.
Topic archived. No new replies allowed.