Adding arithmetic operator overloading to NRVec class


Luke
11-22-2005, 10:52 AM
Hi everyone,

I want to use the +operator with vectors in my program. Therefore, I am trying to add an overloaded operator+ to the NRVec class, by adding the following to the class declaration:

NRVec & operator+(const NRVec &rhs)

Outside the class declaration I write how C++ should interpret use of the + operator with two NRVec's:

template <class T>
NRVec<T> & NRVec<T> :: operator+(const vec &rhs)
{
NRVec<T> result(rhs.size());
for(int i=0;i<rhs.size();i++)
result[i]=v[i]+rhs[i];
return result;
}

However, this does not work; With this code I return an address to a temporary variable "result". I also get a warning from the compiler (VC++ 6).

I have tried declaring a private pointer to a block of dynamic memory in the class declaration, i.e.

T *result;

This does not work either, because upon "return result" it can not assign a block of dynamic memory to an NRVec. Anyone got any ideas on how to solve such a problem? I am sure that anyone who has written their own matrix/vector classes have dealt with this problem...


Thanks,

Luke


A cool quote I found, thought up by and for programmers:
If I asked you to go out with me, would you give me the same answer as the answer to this question?

Luke
11-26-2005, 10:17 AM
I got it, I was returning a reference. Do not return a reference, return the object itself. What I had to realise is that a reference != object. Hence the correct line of code should simply be:

NRVec operator+(const NRVec &rhs)

and

template <class T>
NRVec<T> NRVec<T> :: operator+(const vec &rhs)
{
NRVec<T> result(rhs.size());
for(int i=0;i<rhs.size();i++)
result[i]=v[i]+rhs[i];
return result;
}

Note the absence of the "&" sign. Hope this helps anyone who stumbles upon the same...

Kevin Dolan
11-29-2005, 12:38 PM
Be aware that what you are doing here is extremely inefficient.

Let's say you say something like Vec_DP x = y + z;

What is going to happen is that your + operator will create a temporary block of memory with the result of the addition of y and z, then copy the contents of that temporary vector into x, and then deallocate the temporary block of memory.

And that is if you have a good compilor. What could happen is that it allocates memory in the function for the temporay, and then copies that memory to a vector on the stack (requiring another allocation), and finally copies from the stack to x, and deallocates both the stack vector and the temporary.

What I would recommend is to define a += operator, which would return itself by reference. Such operators can be used very efficiently. Then, if you want a + operator, make it a global function which invokes the += operator, and just be aware that using it is inneficient.

Kevin

Luke
12-05-2005, 06:09 AM
Many thanks for your helpful advice, Kevin. I need to look into that...

I have defined an operator +=, which returns itself as a reference.

When using a global + operator in, say, x = y+z, you would first make a copy x=y. As I understand it, subsequently you would perform x+=z? Is this correct?

Again, thank you for your explanation.

Kevin Dolan
12-07-2005, 01:32 PM
Exactly. That way the + operator does not need to me a member function or friend function of the class, because it is just calling a public method.

And of course, in cases where efficiency is important, you can just use the arithmetic assignment operators, and avoid any unnecessary copying.

Kevin