简体   繁体   English

使数学向量类成为初始化器列表

[英]Make a math vector class to be initializer list aware

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类型的变量:

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: 我听说可以通过实现初始化列表构造函数来使用C ++ 0x缩短此过程,因此可以编写类似以下内容的代码:

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: 以下是我使用g ++ 4.6.1获得的结果:

  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 
  2. Using constructor initializer list & custom copy constructor: 使用构造函数初始化器列表和自定义副本构造函数:

     Median: 634928 ns Median, CPI: 15.8732 ns Average: 636312 ns Average, CPI: 15.9078 ns 
  3. 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. 自定义副本构造函数比默认副本构造函数快35%以上。

Brace-initialization works for all sorts of constructors, and you don't need an initializer-list constructor argument in this case. 括号初始化适用于各种构造函数,在这种情况下,您不需要初始化列表构造函数参数。 On the contrary, initializer lists are for variable content, like the content of a dynamic container, but not for fixed-length constructor arguments. 相反,初始化列表用于变量内容,例如动态容器的内容,但不适用于固定长度的构造函数参数。 So you can just say: 所以你可以说:

vector3D v { 1, 1, 1 };

and all will be fine. 一切都会好起来的。

Note however that you should really initialize your class members rather than assigning them: 但是请注意,您应该真正初始化类成员,而不是分配它们:

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]) { }

You also shouldn't write a copy constructor, since it does no better then the one that's provided by default. 您也不应该编写一个复制构造函数,因为它没有默认情况下提供的更好。 The same goes for the assignment operator. 赋值运算符也是如此。

(Personally, I don't feel comfortable with the float[] constructor; it'd be better to use a std::array<float, 3> ; but then again you might just use such an array as your 3D-vector type from the start and not bother writing a custom class at all.) (就我个人而言,我对float[]构造函数不满意;最好使用std::array<float, 3> ;但是再一次,您可能只使用这样的数组作为3D矢量类型从一开始就不必费心编写一个自定义类。)

Finally, you can combine construct-initializer-lists and initializer-list-constructors in this last example of making a list of vectors: 最后,您可以在生成向量列表的最后一个示例中结合使用construct-initializer-lists和initializer-list-constructors:

std::list<Vector3D> l { { 1.0, 2.0, 3.0}, { 1.5, 3.0, 4.4 }, { 0.0, -1.2, 4.1 } };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM