Skip to main content
GameDev.net gamedev.net
🔒 Locked

overloading operator restrictions

Started by yamaki308 May 2, 2008 at 10:48 PM 9 replies 1.4k views
Original Post
yamaki308
yamaki308
I'm creating a POINT and VECTOR struct with overloading operators for point and vector arithmetic. Here are the declarations for POINT and VECTOR structs: The POINT struct

 namespace Math
 {
	typedef struct _Point
	{
		float mX, mY, mZ;
		
		// constructor
		_Point(void);
		// overloaded constructor
		_Point(const float, const float, const float);
		// overloaded constructor
		_Point(const _Point &);
	
		// assignment operators
		_Point &operator= (const _Point &);
		_Point &operator+= (const _Point &);
		_Point &operator+= (const _Vector &);
		_Point &operator-= (const _Point &);
		_Point &operator*= (const _Point &);
		_Point &operator*= (const float);
			
		// unary operators
		_Point operator- (void);
		_Point operator+ (void);
		
		// binary operators
		_Point operator+ (const _Point &) const;
		_Point operator+ (const _Vector &) const;
		friend _Point operator+ (const _Vector &, const _Point &);
		_Point operator- (const _Point &) const;
		_Point operator* (const _Point &) const;
		_Point operator* (const float) const;
		friend _Point operator* (const float, const _Point &);
		
		// insertion operator
		friend std::ostream &operator<< (std::ostream &, const _Point &);
		
	}POINT;
 }

The VECTOR struct

namespace Math
 {
	typedef struct _Vector
	{
		float mX, mY, mZ;
		
		// constructor
		_Vector(void);
		// overloaded constructor
		_Vector(const float, const float, const float);
		// overloaded constructor
		_Vector(const _Vector &);
	
		// assignment operators
		_Vector &operator= (const _Vector &);
		_Vector &operator+= (const _Vector &);
		_Vector &operator*= (const _Vector &);
		_Vector &operator*= (const float);
		_Vector &operator-= (const _Vector &);
		
		// unary operators
		_Vector operator- (void);
		_Vector operator+ (void);
		
		// binary operators
		_Vector operator+ (const _Vector &) const;
		_Vector operator* (const _Vector &) const;
		_Vector operator* (const float) const;
		friend _Vector operator* (const float, const _Vector &);
		_Vector operator- (const _Vector &) const;
		
		// insertion operator
		friend std::ostream &operator<< (std::ostream &, const _Vector &);
		
	}VECTOR;
 }

I want to get the difference of two points to result a vector like so: VECTOR u = q - p, where p, q are POINT structs. How can I restrict the user to assign any arithmetic operation method except for operator- method for the VECTOR struct?
fastcall22
fastcall22
You're close, but... this might help you restrict:

1. _Point ± _Point shouldn't be possible... But, _Point ± (_Point - origin ) is possible. This ensures _Point strictly for representing location, and _Vector for representing translation.

2. Neither should your * and / operations with _Vector or _Point as the parameter.

3. I also don't think _Point * float doesn't make any sense either... _Vector * float makes sense, but not the former...


Also, this link might help explain why: http://en.wikipedia.org/wiki/Vector_(spatial).


[Edited by - _fastcall on May 3, 2008 12:50:51 AM]
erissian
erissian
If you consider the difference between the fundamental elements and arithmetic for the two objects, I believe that you will agree that there is no particular reason to represent them in two different constructs.

I think that it helps my case that you want the result of point addition to be a vector.

If can explain to me clearly why these should be separate, I'll drop the subject and answer your question.
We''re sorry, but you don''t have the clearance to read this post. Please exit your browser at this time. (Code 23)
yamaki308
yamaki308
I know it's true that _Point * float doesn't make sense because it is not defined as a coordinate independent operation (same goes for the sum of points). However, it can be used for special operations like barycentric combinations and such, although it is used for engineering applications. Also I don't have a operator/ anywhere in my code.
Brother Bob
Brother Bob
Quote:
Original post by yamaki308
I know it's true that _Point * float doesn't make sense because it is not defined as a coordinate independent operation (same goes for the sum of points). However, it can be used for special operations like barycentric combinations and such, although it is used for engineering applications. Also I don't have a operator/ anywhere in my code.

When you start arguing like that, you also end up with two identical classes. So you can just typedef the same class to both a vector and point. Just like erissian said.
yamaki308
yamaki308
you mean something like this, Brother Bob?

typedef struct{ // write code here}VECTOR, POINT;
Brother Bob
Brother Bob
Drop the point and use the vector. If you like, for self documenting code, you can of course provide a typedef for the point to a vector.
yamaki308
yamaki308
as a parameter or a struct, for points I mean?
yamaki308
yamaki308
oh, I get it. something like this: typedef VECTOR POINT, right?
yamaki308
yamaki308
ok, thanks a bunch.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.