Question: I have a vector class that has addition, multiplication, subtraction, division, etc operators. I'm trying to optimize my program (which does a lot of vector operations) and I'm realizing that about 50% of the time spent is in constructing and destructing vectors. I understand that this is because every time I call a non-compound mathematical operator for a vector, a new vector is constructed. Is there a way to prevent this without using compound operators or expanding vector operations?

If I have:

```
Vector a = Vector(x, y, z);
Vector b = Vector(a, b, c);

Vector c = a + b;

```

I can't use += because c is a completely new vector. I know I can speed it up with this:

```
c.x = a.x + b.x;
c.y = a.y + b.y;
c.z = a.z + b.z;

```

but that doesn't seem as clean as just using an operator.

Answer: 
> 
> I understand that this is because
>  every time I call a non-compound
>  mathematical operator for a vector, a
>  new vector is constructed. Is there a
>  way to prevent this without using
>  compound operators or expanding vector
>  operations?
> 
> 
> 

Well, the nature of adding two things together to produce a third thing *requires* you to construct a third thing...so how is what you're asking even logically possible?

That being said, if you're concerned about the temporaries created by using the addition operator, these may be optimized out by the compiler if your compiler supports return value optimization. Alternatively, if your compiler doesn't support this and you really want to cut back on temporaries but keep the `+` operator, you may want to look into emulating C++0x move semantics, and provide your vector with a rvalue move constructor, which will be invoked when it returns a temporary by value. See the Section titled "Moving Objects" in [this article](http://www.ddj.com/cpp/184403806) for information on implementing move semantics in C++03. Once C++0x comes out, you can just replace these hacks with real move constructors using the `&&` operator for rvalue references.
Whoa there. Your code is totally inefficient:

```
Vector a = Vector(x, y, z);
Vector b = Vector(a, b, c);

```

That's just inefficient. What you want to write is

```
Vector a(x, y, z);
Vector b(a, b, c);

```