Holgerson wrote:
Holgerson wrote:
On 2007-10-25 11:08, Holgerson wrote:
Hi everybody,
what I try to do is to implement an operator* into NRVec in such a
way, that I can perform an operation like number*vector rather than
vector*number which is easy. Any ideas? Thanks a lot,
I do not know what NRVec is but I guess that it comes from Numerical
Recipes in C++. First I would like to ask whether the operation you
can do is number vector * number, or vector *= number, the latter
being much more effective since it does not need to create a new
vector.
If there really is a vector * number operator it should be trivial to
add a number * vector operator, it would look something like this:
NRVec operator*(double n, const NRVec& v)
{
return v * n;
}
Since it do not know the specifics of NRVec and which operators it
provides this is the best I can offer. A quick read-up on operator-
overloading should give you all information you need to know to do it
yourself.
--
Erik Wikström
Dear Erik,
thanks for your help and you are right, "NRVec" comes from the
Numerical Recipes. It turns out that your solution has two arguments
which is not allowed for "operator*".
That is incorrect. operator* can be a free standing function (possibly a
friend). In that form, it takes two arguments.
I did in fact implemenet an
operator that performs "vector*number" and this works fine:
declaration:
NRVec operator*(const T &rhs);
and prototype:
template <class T>
NRVec<T> NRVec<T> :: operator*(const T &rhs)
{
NRVec<T> result(nn);
for(int i=0;i<nn;i++)
{
result=v*rhs;
}
return result;
}
However it looks that I'm not smart enough to figure out the
"number*vector" thing. Any ideas?
Use a freestanding operator* with first argument double and second
argument vector.
[snip]
well, I guess that's possible. I just thought I could squeeze it in
into the member functions. When I started out it looked fairly easy. I
didn't quite accomplish it though. As a free standing function may I
still use it like "number*vector"?