tags:

views:

126

answers:

6

I have a Point2D class as follows:

class Point2D{
        int x;
        int y;
    public:
        Point2D(int inX, int inY){
            x = inX;
            y = inY;
        };

        int getX(){return x;};
        int getY(){return y;};
    };

Now I have defined a class Line as:

class Line {
Point2D p1,p2;
public:
 LineVector(const Point2D &p1,const Point2D &p2):p1(p1),p2(p2) {
        int x1,y1,x2,y2;
        x1=p1.getX();y1=p1.getY();x2=p2.getX();y2=p2.getY();
 }
};

Now the compiler gives the error in the last line( where getX() etc are called):

error: passing `const Point2D' as `this' argument of `int Point2D::getX()' discards qualifiers

If I remove the const keyword at both places, then it compiles successfully. What is the error? Is it because getX() etc are defined inline? Is there any way to recify this retaining them inline?

+6  A: 

You have not declared getX() and getY() methods as const. In C++, you can only call const methods from a const object. So your function signature should be int getX() const{..}. By defining them as the const method you are telling the compiler that you are not going to modify any member variable inside this method. Since your object is a const object, it is not supposed to be modified hence you can call only call const methods on it.

Naveen
Why do I need to declare them as constant? Please elaborate.
avd
If you don't the compiler assumes getX() has side effects or alters your object. Your LineVector constructor calls getX() on const Point2D objects, and you are not allowed to change a const object/reference. You have to tell the compiler that it's ok to call getX() - by marking the method as const. You can only call const methods on a const object.
nos
+2  A: 

To guarantee the constness of the parameter, you can only call const methods on them, because otherwise the compiler couldn't be sure that the methods don't change the value. Declare getX and getY as const, like this:

    int getX() const {return x;}
    int getY() const {return y;}

Note that the semicolon after the closing curly brace is not necessary.

OregonGhost
+1  A: 

try define your getX function as

int getX() const {...}
Vladimir
+1  A: 

you have to declare your getX(), getY() functions as const too:

int getX() const {return x;}
int getY() const {return y;}

the const keyword tells the compiler that the methods will not change the state of the object

knittl
+2  A: 

It's because getX() etc are not const. You could define them like this:

    int getX() const {return x;};
    int getY() const {return y;};
  //  ---------^^^^^

Since p1 and p2 are const references, one must only call const methods. But without the const modifier in getX(), they are assumed to modify this (i.e. p1 and p2), which is not allowed.

KennyTM
+1  A: 

Declare your member functions to be "const" if they don't modify the state.

class Point2D{
    int x;
    int y;
public:
    Point2D(int inX, int inY){
        x = inX;
        y = inY;
    };

    int getX() const {return x;};
    int getY() const {return y;};
    //         ^^^^^
};

This is about the hidden this parameter. Either this will be of type const Point2D* or Point2D*. If you have a reference to a const Point2D you can't invoke non-const member functions because there is no implicit conversion from const Point2D* to Point2D*.

sellibitze