Sign in to follow this  
Porco_Rosso

C++ Operator overloading

Recommended Posts

Porco_Rosso    122
Hello - I was wondering if anybody had the answer to this problem - I am making a few vector classes in C++ - and i have been happily overlaoding all of their operators - however when casting between vector types i have a problem if the vector i am casting is const ... e.g pointPosition = (ts16Vector2)*pDrawing->GetPoint(point); where GetPoint returns a vector of type ts8Vector* - the problem is that my casting func - e.g. ts16Vector2::operator ts8Vector2() { ts8Vector2 returnVec; returnVec.x = (s8)x; returnVec.y = (s8)y; return returnVec; }; uses implicit "this" which I guess is not const - anyway the compiler complains saying its invalid and if the vector I want to cast is not const it works fine..?! anybody know? thanks, Ross

Share this post


Link to post
Share on other sites
Sharlin    864
ts16Vector2::operator ts8Vector2() const

This will pass the this pointer as a constant. You should liberally mark all non-mutating memberfunctions as such.

Share this post


Link to post
Share on other sites
Random question: Why is GetPoint() returning a pointer? Can it ever return NULL? If so, shouldn't you be checking for NULL? If not, shouldn't you at least return by reference, or (better yet) return by value?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this