C++ seperate assignment or object assignment

Started by
7 comments, last by iMalc 18 years, 4 months ago
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?
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
Advertisement
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
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?
Quote:Original post by me22
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?


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.
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
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.
Quote:Original post by Conner McCloud
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 ...
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) ;
--> The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones <--
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;}
Quote:Original post by load_bitmap_file
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.


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) ); )
The examples you gave are the same, but I suggest you look into Expression Templates.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement