Sign in to follow this  
Endar

C++ seperate assignment or object assignment

Recommended Posts

Endar    668
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 this post


Link to post
Share on other sites
Conner McCloud    1135
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 this post


Link to post
Share on other sites
me22    212
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 this post


Link to post
Share on other sites
Endar    668
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.

Share this post


Link to post
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 this post


Link to post
Share on other sites
Skeleton_V@T    512
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) ;

Share this post


Link to post
Share on other sites
Sharlin    864
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 this post


Link to post
Share on other sites
me22    212
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) ); )

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this