C++ Operator overloading

Started by
2 comments, last by JasonBlochowiak 17 years, 5 months ago
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
Advertisement
ts16Vector2::operator ts8Vector2() const

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

that was the problem!
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?

This topic is closed to new replies.

Advertisement