Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

pluto_knight

What's wrong with this segment of code?

This topic is 5491 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

class Point2d { public: Point2d(float x = 0.0, float y = 0.0) : _x(x), _y(y) {}; float x() { return _x; } float y() { return _y; } void x(float newX) { _x = newX; } void y(float newY) { _y = newY; } void Point2d::operator += ( const Point2d & rhs) { _x += rhs.x(); _y += rhs.y(); } protected: float _x, _y; }; If I eliminate the keyword const, it will be ok. But why?

Share this post


Link to post
Share on other sites
Advertisement
i think you should declare the methods x() and y() as const, so the compiler knows they are not changing any contents of the class:


class Point2d {
public:
Point2d(float x = 0.0, float y = 0.0)
: _x(x), _y(y) {};

float x() const { return _x; }
float y() const { return _y; }

void x(float newX) { _x = newX; }
void y(float newY) { _y = newY; }
void Point2d::operator += ( const Point2d & rhs) {
_x += rhs.x();
_y += rhs.y();
}

protected:
float _x, _y;
};

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!