tags:

views:

96

answers:

1

I don't understand why the below code does not build:

bool AguiRectangle::pointInside(const AguiPoint &p )
{
    if(p.getX() < x) return false;
    if(p.getY() < y) return false;
    if(p.getX() >= x + width) return false;
    if(p.getY() >= y + height) return false;
    return true;

}

I get this:

Error   1   error C2662: 'AguiPoint::getX' : cannot convert 'this' pointer from 'const AguiPoint' to 'AguiPoint &'  c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\aguibasetypes.cpp 259
Error   3   error C2662: 'AguiPoint::getX' : cannot convert 'this' pointer from 'const AguiPoint' to 'AguiPoint &'  c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\aguibasetypes.cpp 261
Error   2   error C2662: 'AguiPoint::getY' : cannot convert 'this' pointer from 'const AguiPoint' to 'AguiPoint &'  c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\aguibasetypes.cpp 260
Error   4   error C2662: 'AguiPoint::getY' : cannot convert 'this' pointer from 'const AguiPoint' to 'AguiPoint &'  c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\aguibasetypes.cpp 262
+12  A: 

The functions AguiPoint::getX() and AguiPoint::getY() need to be defined as const member functions or you won't be able to call them on const AguiPoints.

You attempted to call a non-const member function on p, which is reference to a const AguiPoint. Since references are aliases to the original object, calling a non-const member function on a const AguiPoint & is equivalent to calling a non-const member function on a const AguiPoint, which is an error.

It's an error because since the method was not declared const, there's no guarantee that it won't modify the const AguiPoint instance. Without this guarantee, the compiler must assume that it will somehow modify the const AguiPoint object, and attempting to modify a constant object is a bad thing, i.e. a violation of const-correctness. A compiler that reasonably conforms to the C++ standard must issue a diagnostic in this case.

Assuming that AguiPoint::getX() and AguiPoint::getY() are just inspector methods of the class, they really should be const anyway since they don't modify the data inside AguiPoint:

class AguiPoint
{
public:
    // Inspector methods - they don't change the data of an AguiPoint.
    int getX() const { /* Implementation */ } // Note const member function
    int getY() const { /* Implementation */ }
};

Also, since the function AguiRectangle::pointInside() doesn't modify any data members of AguiRectangle, you should define AguiRectangle::pointInside() as a const member function as well:

bool AguiRectangle::pointInside(const AguiPoint &p ) const // Note const
{
    /* Implementation */
}

Const-correctness is an important and fundamental concept in C++, so be sure to read up on this FAQ on const-correctness and the Guru of the Week article #6. There's even a pretty good Wikipedia article on const-correctness.

In silico