C++ seperate assignment or object assignment

Recommended Posts

Wondering if it is better to do seperate assignment with each data member, or create a new assignment and use the overloaded operator+ in a situation as follows:
class CVector3d
{
public:
float x,y,z;
};

// is it better to do
vector.x = 0;
vector.y = 1;
vector.z = 2;

// or
vector = CVector3d(0,1,2);


Which is better? Or does it not matter because the creation of an object with only 3 floats and without dynamic memory is fairly trivial?

Share on other sites
I prefer the constructor in order to prevent errors...you can't forget to assign a value to z, or accidentally assign a value to x twice. In terms of performance, I doubt very seriously that it matters, but a quick test should reveal one way or the other. Implement both, of course, because you probably want access to your components anyhow.

CM

Share on other sites
Assigning individually is probably the "fastest".

That being said, initialising shouldn't be a bottleneck, so the assignment is probably the best, since it's the clearest. Also, good compilers should be able to apply the Return Value Optimization in that case which would mean no temporary at all.

As always, profile. And please don't put a C in front of your class names. It's really not helpful.

In any case, "fastest" also means you should probably be storing the coordinates in an array anyways, you can pass them by the pointer. Perhaps you need a more complete vector<> class?

Share on other sites
Quote:
 Original post by me22In any case, "fastest" also means you should probably be storing the coordinates in an array anyways, you can pass them by the pointer. Perhaps you need a more complete vector<> class?

I already use OGL vertex arrays for rendering. [smile] This is just for testing intersections between rays and AABB's. Probably won't make a real difference, but it was something I had been wondering, so it seemed like the time to ask.

Thanks.

Share on other sites
There's also initialization for aggregate types such as your class where you can do: CVector3d vector = {0, 1, 2}; to initialize. It's useful for very simple types such as this.

I wouldn't worry about speed in this situation, any of the above methods will probably be more or less the same speed as any other.

Share on other sites
Quote:
 Original post by Conner McCloudI prefer the constructor in order to prevent errors...you can't forget to assign a value to z, or accidentally assign a value to x twice ...CM

And even runtime speed is not that matters, it matters when you have to type three lines of code instead of one simple line[smile]. Imagine you assign values to a 4x4 matrix [wink], this will be a real pain doing it.

Oh well[smile], multiple assignments is possible for the latter:
vec1 = vec2 = ... = vecn = CVector3d(0, 0, 0) ;

Share on other sites
Even though most of the overhead involved in constructing and copying the temporary Vector is probably optimized out, I'd prefer an "assign" member function a la std::string:
CVector3d &CVector3d::assign(float x, float y, float z){  this.x = x; this.y = y; this.z = z;  return *this;}

Share on other sites
Quote:
 Original post by load_bitmap_fileThere's also initialization for aggregate types such as your class where you can do: CVector3d vector = {0, 1, 2}; to initialize. It's useful for very simple types such as this.

However for that to work, you must be using all public data and no custom constructors at all, which means it's impossible to initialise it inline. ( for something like float projection = dot( my_vec, Vector3d(1,1,1) ); )

Share on other sites
The examples you gave are the same, but I suggest you look into Expression Templates.

Create an account

Register a new account

• Forum Statistics

• Total Topics
628367
• Total Posts
2982280

• 10
• 9
• 13
• 24
• 11