Compiler Error 2662 - problems using 'const'

Hi,

I have a simple class declared:

class CBall {
      UINT      radius;
      int      dx, dy;
      BallColour      colour;
      CPoint      position;      
public:      
      CBall(const UINT radius, const CPoint position, const BallColour colour);
      CBall();

      // Adjust position/dx/dy accordingly
      void Update();
      void CollideX();
      void CollideY();

      CPoint GetCentre() {return position;}

      const int operator == (const CBall ball);
      const int operator != (const CBall ball);

      const BallColour GetColour() const {return colour;}
      const int GetRadius() const {return radius;}
      
};

Compilation generates the following error:

error C2662: 'GetCentre' : cannot convert 'this' pointer from 'const class CBall' to 'class CBall &'
        Conversion loses qualifiers

If I declare GetCentre as a 'const' function there is no error - but why do I need to do this.. why is the 'this' pointer constant?

Many Thanks,

AT.
Attack_TraxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

stefan73Commented:
Hi Attack_Trax,
CPoint probably is a structure instead of a primitive data type. You should use

const CPoint& GetCentre() {return position;}
or
const CPoint& GetCentre() {return position;}


instead.


Cheers!

Stefan
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pb_indiaCommented:
You are constructing  with const variables:

Try this

CBall(UINT radius, CPoint position, BallColour colour);
0
Attack_TraxAuthor Commented:
Hi Stefan.

Thanks for your reply - however changing the return type of GetCentre() to 'const CPoint&' does not prevent the error.

Why do I need to declare the body  {return position;} as being constant to prevent this error?

Thanks again,

AT.
0
Attack_TraxAuthor Commented:
Sorry to waste your time - I have figured it now! The method was being called from a constant CBall instance.. and thus would only allow constant methods to be invoked. Obvious once I worked out where the problem was...

Now it all makes sense :-)

I will split the points for you both - thanks for replying.

AT.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.