Sign in to follow this  

operator overloading problem

Recommended Posts

Please help me, here is the code: Vectorx3f *operator -(const Vectorx3f *u, const Vectorx3f *v) { return new Vectorx3f(u->x-v->x,u->y-v->y,u->z-v->z); } Error Log: `Vectorx3f* operator-(const Vectorx3f*, const Vectorx3f*)' must have an argument of class or enumerated type.

Share this post

Link to post
Share on other sites
You cannot overload primitve types,and pointers to types count as primitives in this respect.

Pointers to stuff must obey the rules of pointer arithmetic.

Anyway, returning a pointer from a metohd like that is a bad idea, it could cause lots of memory leaks.

A better idea would be:

Vectorx3f operator -(const Vectorx3f &u, const Vectorx3f &v)
return Vectorx3f( u.x - v.x, u.y - v.y, u.z - v.z );

[edit: yeah they aren't pointers anymore, thanks jflanglois! ]

Share this post

Link to post
Share on other sites
Original post by krum
You can't do that with pointers. You must use a reference.

Vectorx3f operator -(const Vectorx3f &u, const Vectorx3f &v)



Returning by reference is bad unless the operator is an assignment one, as the return value is a temporary, and using a temporary reference out of scope is undefined (I think).

Share this post

Link to post
Share on other sites
It is normal to implement binary arithmetic operators in terms of immediate arithmetic operators:

// The immediate operator is supposed to mutate the this-object:
// "subtract rhs from self". A reference to self is returned so that the
// operator can chain in the usual way. That chaining is rarely used in normal
// code, but it is sometimes used, and will actually help with the other
// operator implementation.
// We accept a const reference because the thing we subtract doesn't change,
// just the thing we subtract *from*; but we also have to accept an instance
// (not a pointer) and we don't want to copy it.
Vectorx3f& Vectorx3f::operator-=(const Vectorx3f& rhs) {
x -= rhs.x; y -= rhs.y; z -= rhs.z;
return *this;

// For the binary operator, we are yielding a separate value which represents
// the difference between the inputs. That calls for returning by value. We
// still accept by const reference for the reasons cited above.
// What we will do is make a copy of the lhs, using the copy constructor,
// subtract rhs from the copy with that immediate operator, and yield the result.
Vectorx3f operator-(const Vectorx3f& lhs, const Vectorx3f& rhs) {
return Vectorx3f(lhs) -= rhs;
// The -= yields a reference, but by returning by value it is implicitly
// dereferenced for us. The compiler should be good enough to optimize away
// any copying - beyond the initial copy, which is necessary because we
// return a separate object.

EDIT: Clearly I am the moron here. Thanks. :)

[Edited by - Zahlman on May 19, 2006 10:46:06 AM]

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