I have a math vector class that is designed as follows:
class Vector3D {
public:
float x;
float y;
float z;
public:
Vector3D() {}
Vector3D(float xx, float yy, float zz = 0.0) { x=xx; y=yy; z=zz; }
Vector3D(const float v[]) { x=v[0]; y=v[1]; z=v[2]; }
Vector3D(const Vector3D& v) { x=v.x; y=v.y; z=v.z; }
// math member methods
// ...
};
I used to use the following to create a Vector3D-type variable on the stack:
Vector3D vec1 = Vector3D(1.0, 1.0, 1.0);
I heard a can shorten this up with C++0x by implementing an initializer list constructor, so it will be possible to write something like:
Vector3D vec1 = { 1.0, 1.0, 1.0 };
What is the right way to implement this?
Update
The curly braces syntax really works out of the box for this class! Thanks for the answer and the comments!
Also, I did some synthetic performance tests trying to measure if constructor initializer list gives a speedup over member variable assignment in a constructor. Below is the results I've got with g++ 4.6.1:
As is (member assignment in a constructor & custom copy constructor):
Median: 634860 ns Median, CPI: 15.8715 ns Average: 636614 ns Average, CPI: 15.9154 ns
Using constructor initializer list & custom copy constructor:
Median: 634928 ns Median, CPI: 15.8732 ns Average: 636312 ns Average, CPI: 15.9078 ns
Using constructor initializer list & default copy constructor:
Median: 860337 ns Median, CPI: 21.5084 ns Average: 864391 ns Average, CPI: 21.6098 ns
Some of the conclusions:
- The constructor initializer list does not give a speedup over member variable assignment in the case of the math vector class presented above.
- The custom copy constructor is more than 35% faster than the default copy constructor.