operator overloading problem
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.
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:
[edit: yeah they aren't pointers anymore, thanks jflanglois! ]
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! ]
You can't do that with pointers. You must use a reference.
Vectorx3f operator -(const Vectorx3f &u, const Vectorx3f &v)
:D
John
Vectorx3f operator -(const Vectorx3f &u, const Vectorx3f &v)
:D
John
Quote:Original post by rip-offVectorx3f operator -(const Vectorx3f &u, const Vectorx3f &v){ return Vectorx3f(u.x-v.x,u.y-v.y,u.z-v.z);}
(replace -> with .)
jfl.
Quote:Original post by krum
You can't do that with pointers. You must use a reference.
Vectorx3f operator -(const Vectorx3f &u, const Vectorx3f &v)
:D
John
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).
It is normal to implement binary arithmetic operators in terms of immediate arithmetic operators:
EDIT: Clearly I am the moron here. Thanks. :)
[Edited by - Zahlman on May 19, 2006 10:46:06 AM]
// 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]
Just to be completely clear...
Vectorx3f& Vectorx3f::operator-=(const Vectorx3f& rhs) { x -= rhs.x; y -= rhs.y; z -= rhs.z; return(*this); }
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement