New to vectors, will this work correcty?

Started by
23 comments, last by Programmer16 20 years, 1 month ago
quote:Original post by gspdriver
Use the second assignment operator...ALWAYS!!!
Why? Because it is more efficient.

Nope. See above comment about spurious temporary objects being constructed.

quote:
You need to add more thing into your assignment operator.
You need to check if you are copying to the same location.

Checking for self-assignment for a simple class like this is counter-productive. Self-assignment in a 3d vector class is unlikely to occur that frequently, so its likely waste cycles for evaluating the conditional. Not only that, but due to branch prediction, you''re likely going to cause a nice pipeline bubble even when you do skip the assignments, so you won''t gain a cycles. There''s also the fact that you''ll be polluting the branch prediction cache, causing other branch predicitons to be less accurate; thus reducing the overall performance of your application.
Advertisement
quote:Original post by psykr
Does it make any difference if you return a const reference instead of a non-const reference from the assignment operator?

Returning a non-const reference is usually preferred. In a few very limited situations you might want to return a non-const reference. (So limited, that I cannot in fact think of any situation where it would be better off the top of my head.)
Hi again!

I just wanted to apologize for my misbehaviour. I was really tired and nearly brain-dead .

The links seem to be very good, I''m glad you''re working it out, programmer16 .

And don''t ever stop trying things out because of mean guys like me *fg*



Indeterminatus

--si tacuisses, philosophus mansisses--
Indeterminatus--si tacuisses, philosophus mansisses--
No problem (my behavior wasn''t that great either). I see why you said not to use the switch statement now. I got everything working fin for now. One question though, how come D3DXVECTOR3 doesn''t have an = operator (or does D3DXVECTOR have one)? Thanks for all your help guys!

/*
I use DirectX 8.1 and C++ (Microsoft Visual C++ 6.0 Professional edition)
*/
D3DXVECTOR3 doesn''t explicitly define an assignment operator because the one that the compiler automatically generates is good enough.

This topic is closed to new replies.

Advertisement