Custom Vector Class (Am I Doing Everything Correctly?)

Started by
18 comments, last by ToohrVyk 16 years, 9 months ago
I'm returning to OpenGL and C++ after a six or seven month hiatus caused mainly by the release of XNA and my discovery of the wonders of the managed world. Needless to say, I do prefer C# and managed code, but I wanted to start doing more programming in OS X since I'm gettting a new MacBook Pro soon. That long and unnecessary intro out of the way... The first thing I figured I'd tackle was writing up some basic structures for my "engine" of sorts. Figured tackling a 2D vector class would be an appropriate first step returning to the language. So I have the "basics" taken care of, in that I have the constructors and most (if not all) of the needed operators overloaded. I just wanted someone to make sure I'm doing everything in a kosher way. Mainly I was wondering if I was using 'const' properly or not as that's something that's always tripped me up. Also am I returning the proper types from my operators? Thanks a lot to anyone who takes a few seconds and lets me know what I can do to improve.

namespace Island
{
	class Vector2
	{
	public:
		GLfloat X, Y;
		
		Vector2() : X(0), Y(0) { }
		Vector2(GLfloat x, GLfloat y) : X(x), Y(y) { }
		Vector2(const Vector2 &v) : X(v.X), Y(v.Y) { }
		
		Vector2& operator = (const Vector2 &v)
		{
			X = v.X;
			Y = v.Y;
			return (*this);
		}
		
		Vector2& operator += (const Vector2 &v)
		{
			X += v.X;
			Y += v.Y;
			return (*this);
		}
		
		Vector2& operator -= (const Vector2 &v)
		{
			X -= v.X;
			Y -= v.Y;
			return (*this);
		}
		
		Vector2& operator *= (float s)
		{
			X *= s;
			Y *= s;
			return (*this);
		}
		
		Vector2& operator /= (float s)
		{
			X /= s;
			Y /= s;
			return (*this);
		}
		
		Vector2 operator + (const Vector2 &v) const
		{
			return Vector2(X + v.X, Y + v.Y);
		}
		
		Vector2 operator - (const Vector2 &v) const
		{
			return Vector2(X - v.X, Y - v.Y);
		}
		
		Vector2 operator * (float s) const
		{
			return Vector2(X * s, Y * s);
		}
		
		Vector2 operator / (float s) const
		{
			return Vector2(X / s, Y / s);
		}
		
		
	};
}


Advertisement
Assignment operators (=, +=, /=, etc) should return const references (const Vector2 &.)

Also, it might be a good idea to check for divide by zero in your / and /= operator overloads.
Thanks. I'll go ahead and put those in there. Anyone else see anything?
You should add a non member operator*(float s, const Vector2 &v) to allow scalar multiplication on the left.
Assignment operators don't have to return const references (and I wouldn't expect them to). You're missing functions for the length of your vector, normalization, dot product and polar representation.

Aside from that, it's a correct naive implementation of a 2D vector in C++.
Quote:Original post by ToohrVyk
Assignment operators don't have to return const references (and I wouldn't expect them to). You're missing functions for the length of your vector, normalization, dot product and polar representation.

Aside from that, it's a correct naive implementation of a 2D vector in C++.


I put those in, but I wanted to make sure I was grasping the rest of it first. I methods for length, normalization, and all the rest that you mentioned.

Why wouldn't assignment operators return a const reference? Seems like it should return at least a normal reference so you can chain assignments together if you wanted:

Vector a(2, 5);
Vector b(5, 3);
Vector c = b = a;

If you ever wanted to do that, it would have to return a reference of some sort. Or are you referring to the +=, -=, *=, and /=? Those I can see why they don't need it an I'll probably remove them.

My main concern is just getting used to C++ again. I used to use it every day for a few years, but the last six months have been all C# so I just wanted to check on the references and consts. Thanks to all for the input.
I think the point about not returning const references from assignment operators might be more about not returning anything. Being able to write "a = b = c = 10;" might look clever and that, and obviously it's permitted by the standard, but it isn't that useful. Writing the assignments separately is rather easier to read.

I switched to no-return assignment operators and it made me much happier.

My 2p.

Will
So it's more or less a personal choice as to whether or not to return the reference? I guess I'll have to ponder which I prefer. I suppose it's always easier to add in the returning of a reference later as opposed to building around the idea of having it and later removing it. So to not have it now, I could always add it in later if I really see a need.
Quote:
Original post by NickGravelyn
Seems like it should return at least a normal reference so you can chain assignments


Chained assignments will work with both const and non-const references. When you write 'a = b = c', the compiler parses it right to left, so it first assigns c to b, returns a reference, which is then assigned to a. So the reference returned can be const and it will still work.
Quote:Original post by NickGravelyn
Why wouldn't assignment operators return a const reference?


My sentence wasn't very clear. My intent was to say that the returned references don't have to be constant.

This topic is closed to new replies.

Advertisement