在 C++ 中返回一个浮点数组
我目前在 C++ 中有一个 4x4 矩阵类,并将每个值存储为浮点数:
Matrix4d::Matrix4d(const float& m00, const float& m01, const float& m02, const float& m03,
const float& m10, const float& m11, const float& m12, const float& m13,
const float& m20, const float& m21, const float& m22, const float& m23,
const float& m30, const float& m31, const float& m32, const float& m33)
{
_m00 = m00;
_m01 = m01;
_m02 = m02;
_m03 = m03;
_m10 = m10;
_m11 = m11;
_m12 = m12;
_m13 = m13;
_m20 = m20;
_m21 = m21;
_m22 = m22;
_m23 = m23;
_m30 = m30;
_m31 = m31;
_m32 = m32;
_m33 = m33;
}
我的问题是,如何返回此数据的浮点数组?例如,我在类中创建数组没有问题:
float arrayToReturn[16] = { m00, m01, m02, m03, ... m33 };
但是我无法从类中返回该值。我读过有关返回指向数组的指针的内容,但没有成功。
I currently have a 4x4 matrix class in C++ and I store each value as a float:
Matrix4d::Matrix4d(const float& m00, const float& m01, const float& m02, const float& m03,
const float& m10, const float& m11, const float& m12, const float& m13,
const float& m20, const float& m21, const float& m22, const float& m23,
const float& m30, const float& m31, const float& m32, const float& m33)
{
_m00 = m00;
_m01 = m01;
_m02 = m02;
_m03 = m03;
_m10 = m10;
_m11 = m11;
_m12 = m12;
_m13 = m13;
_m20 = m20;
_m21 = m21;
_m22 = m22;
_m23 = m23;
_m30 = m30;
_m31 = m31;
_m32 = m32;
_m33 = m33;
}
My question is, how can I return a float array of this data? I have no problem creating the array in the class for example:
float arrayToReturn[16] = { m00, m01, m02, m03, ... m33 };
However I can not return this value from the class. I've read about returning a pointer to the array, but have had no luck with it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您的内部数组看起来像
float array[4][4]
,那么这将起作用:如果您的内部数组是一维数组:
但这两种情况都会将类的内部工作方式暴露给外界,使您的代码更不安全且更难以维护。
最好为 Matrix4d 类创建一个复制构造函数、一个
()
运算符和一个赋值运算符,然后将其传递出去。由于内存管理不良或数据损坏而出现运行时错误的可能性较小。您的
()
运算符将如下所示:您可以这样调用它来设置值:
或者这样调用它来检索:
它是两种方式。
编辑:忘记了
[]
运算符只接受一个参数。将运算符更改为()
运算符,也称为函数运算符。This would work if your internal array looked like
float array[4][4]
:If your internal array was a one-dimensional array:
But both cases expose the internal workings of your class to the outside world, with makes your code less safe and harder to maintain.
It would be better to create a copy constructor, a
()
operator, and an assignment operator for your Matrix4d class and just pass that around. You'd be less likely to have runtime errors due to bad memory management or data corruption.Your
()
operator would look like this:You would call it like this for setting values:
or this for retrieving:
It works both ways.
EDIT: Forgot that the
[]
operator only took one argument. Changed the operator to the()
operator a.k.a the functional operator.您可以使用联合来描述您的班级。
然后你可以返回_m。
获取 cols 也很有用,因此:
You can use a union to describe your class.
You can then return _m.
also getting cols can be useful so:
我能想到的三个选择。
第一个是返回 std::vector 而不是数组。
用户总是可以通过&v[0]获取指向内部数组的指针。
第二个是返回一个 boost::array 。 (我认为如果您的编译器支持类似的东西,即将推出的 C++0x 标准中有一个 tr1::array )同样可以轻松访问内部数组。
第三种是最麻烦的,但如果您需要速度,可能是最好的。如果您连续存储浮点数,则只需返回指向第一个条目的指针即可。 (请注意,您需要小心类的详细信息,否则您最终会得到“未定义的行为”。但是,许多/大多数/所有编译器无论如何仍然会“做正确的事情”。)哦,您需要小心,因为仅当矩阵仍然存在时,指针才有效。
Three options I can think of.
The first is return a std::vector rather than an array.
The user can always get pointer to the internal array by &v[0].
The second is return a boost::array. (I think there's a tr1::array from the upcoming C++0x standard if your compiler supports something like that) Again its easy to get to the internal array.
The third is the hackiest but may best if you need speed. If you're storing your floats contiguously you could just return a pointer to the first entry. (Note you need to be careful about the details of your class otherwise you end up with "undefined behaviour". However, many/most/all compilers will still "do the right thing" anyway.) Oh and you need to be careful as the pointer will only be valid while the matrix still exists.
只需访问第一个元素的地址即可。
您可以将 任何结构视为一个数组。
Just access the address of the first element.
You can treat any struct as an array.
不要通过 const 引用传递浮点数,而是按值传递它们。
我假设您想返回数组以便可以进行索引?然后不要从矩阵类返回数组。相反,重载
[]
运算符或其他内容。此外,我不会使用 16 个成员变量,而是使用一个数组。使索引变得更加容易。
我可能会这样做:
Don't pass floats by const reference, pass them by value.
I assume you want to return the array so you can do indexing? Then don't return an array from you matrix class. Instead, overload the
[]
operator or something.Also, I wouldn't use 16 member variables but one array instead. Makes indexing a lot easier.
Here is how I would probably do it: