using const return value for method..

Hi all,

//////////////// Vector class //////////////////

In Vector.h:
1
2
3
4
5
6
7
8
9
10
Class Vector
{
private:
	int X;
	int Y;
public:
	Vector(int x , int Y);
	int getX();
	int getY();
};


In Vector.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
Vector::Vector(int x , int Y)
{
	this->X = x;
	this->Y = y;
}
int Vector::getX()
{
	return X;
}
int Vector::getY()
{
	return Y;
}


/////////////////// Polygon class //////////////////

In Polygon.h
1
2
3
4
5
6
7
8
class Polygon
{
public:
	Polygon();	
	const Vector* getPolygonVector();
private:
	Vector* pointVect;
}


In Polygon.cpp
1
2
3
4
5
6
7
8
Polygon::Polygon()
{
	pointVect = new Vector(0 , 0);
}
const Vector* Polygon::getPolygonVector()
{
	return pointVect;
}


/////////////// CollisionDetection class ///////////////

In CollisionDetection.h
1
2
3
4
5
6
7
class CollisionDetection
{
private:
	Polygon* myPoly;	
public:
	static void detect();
}


In CollisionDetection.cpp

1
2
3
4
5
6
void CollisionDetection::detect()
{
	const Vector* temp = myPoly->getPolygonVector();
	int tempX = temp->getX();
	int tempY = temp->getY();
}


In the above code in CollisionDetection::detect() method for the following two lines of code
1
2
int tempX = temp->getX();
int tempY = temp->getY();

it is throwing following error
error: passing 'const Vector' as 'this' argument of 'float Vector::getY()' discards qualifiers

Can any one please tell me how can I over come this issue??
Thanks in advance,
Madhu.
To make member functions work with const object, you have to say that they won't modify it:
int getX(); // the compiler will think you are going to modify the object

int getX() const; // the compiler will know that it works with const object
Hi Bazzy,
Thanks for the reply.

I will try that one.

-Madhu.
Topic archived. No new replies allowed.