C++ 将类转换为固定长度浮点数组
如何将以下类转换为固定长度的浮点数组C++ 将类转换为固定长度浮点数组,c++,C++,如何将以下类转换为固定长度的浮点数组 class Vertex { public: Vertex( float x = 0, float y = 0, float z = 0) : x(x), y(y), z(z) {} float x, y, z; }; 例如,我想这样使用它: Vertex v(0, 1, 0); float arr[3] = v; // How to convert here? 谢谢 编辑: 我
class Vertex
{
public:
Vertex( float x = 0,
float y = 0,
float z = 0)
: x(x), y(y), z(z) {}
float x, y, z;
};
例如,我想这样使用它:
Vertex v(0, 1, 0);
float arr[3] = v; // How to convert here?
谢谢
编辑: 我应该在发布这个问题之前添加一些背景信息 我之所以使用C样式数组,是因为我想将高级顶点对象组合成一个顶点数组,以便使用OpenGL进行渲染,据我所知,这需要一组原始数组(
float[3]
)或结构
为此,我认为这是最好的选择。但是,如果存在更优雅的解决方案,那就更好了。:) > P>类不能转换为(原始)数组,因为CAST运算符必须返回一个数组,这在C++中是不允许的。此外,无论如何都不能对数组进行复制初始化 您可以定义一个数组,并将其传递给一个函数,该函数根据对象的内容填充数组:
void Vertex::fill_arr(float (&arr)[3]) {
arr[0] = x;
arr[1] = y;
arr[2] = z;
}
// usage
Vertex v(1, 2, 3);
float arr[3];
v.fill_arr(arr);
另一种选择是使用
#include <iostream>
#include <array>
using namespace std;
class Vertex
{
public:
Vertex( float x = 0,
float y = 0,
float z = 0)
: x(x), y(y), z(z) {}
operator array<float, 3>() const {
return {x,y,z};
}
/* See @user2079303's answer for a compile-time check of the array dimension */
void fillArray(float arr[3]) {
arr[0] = x;
arr[1] = y;
arr[2] = z;
}
float x, y, z;
};
int main() {
Vertex v(1,1.4,2);
array<float, 3> arr = v;
float arr2[3];
v.fillArray(arr2);
for (int i = 0; i < 3; i++) {
cout << arr[i] << " " << arr2[i] << endl;
}
return 0;
}
您可以定义一个数组,并将其传递给一个函数,该函数根据对象的内容填充数组:
void Vertex::fill_arr(float (&arr)[3]) {
arr[0] = x;
arr[1] = y;
arr[2] = z;
}
// usage
Vertex v(1, 2, 3);
float arr[3];
v.fill_arr(arr);
另一个选项是使用std::array
,它可以正常返回和复制初始化
#include <iostream>
#include <array>
using namespace std;
class Vertex
{
public:
Vertex( float x = 0,
float y = 0,
float z = 0)
: x(x), y(y), z(z) {}
operator array<float, 3>() const {
return {x,y,z};
}
/* See @user2079303's answer for a compile-time check of the array dimension */
void fillArray(float arr[3]) {
arr[0] = x;
arr[1] = y;
arr[2] = z;
}
float x, y, z;
};
int main() {
Vertex v(1,1.4,2);
array<float, 3> arr = v;
float arr2[3];
v.fillArray(arr2);
for (int i = 0; i < 3; i++) {
cout << arr[i] << " " << arr2[i] << endl;
}
return 0;
}
#包括
与使用C样式阵列一样高效,不会损失性能。您也可以改用std::vector
即使是在C语言中,也不能只返回和复制数组。这就是为什么如果你绝对想使用C数组,你必须有一个类似于fillArray
\include的函数
#包括
与使用C样式阵列一样高效,不会损失性能。您也可以改用std::vector
即使是在C语言中,也不能只返回和复制数组。这就是为什么如果你绝对想使用C数组,你必须有一个类似于fillArray
的函数。你可以定义一个转换操作符来构造你的数组。我还建议使用std::array
而不是原始数组
#include <array>
class Vertex
{
public:
Vertex(float x = 0.0f, float y = 0.0f, float z = 0.0f)
: x(x), y(y), z(z)
{}
float x;
float y;
float z;
operator const std::array<float, 3>() const
{
return {x, y, z};
}
};
int main()
{
Vertex v(0.0f, 1.0f, 0.0f);
std::array<float, 3> arr = v;
}
#包括
类顶点
{
公众:
顶点(浮动x=0.0f,浮动y=0.0f,浮动z=0.0f)
:x(x),y(y),z(z)
{}
浮动x;
浮动y;
浮动z;
运算符常量std::数组()常量
{
返回{x,y,z};
}
};
int main()
{
顶点v(0.0f,1.0f,0.0f);
std::数组arr=v;
}
您可以定义转换运算符来构造数组。我还建议使用std::array
而不是原始数组
#include <array>
class Vertex
{
public:
Vertex(float x = 0.0f, float y = 0.0f, float z = 0.0f)
: x(x), y(y), z(z)
{}
float x;
float y;
float z;
operator const std::array<float, 3>() const
{
return {x, y, z};
}
};
int main()
{
Vertex v(0.0f, 1.0f, 0.0f);
std::array<float, 3> arr = v;
}
#包括
类顶点
{
公众:
顶点(浮动x=0.0f,浮动y=0.0f,浮动z=0.0f)
:x(x),y(y),z(z)
{}
浮动x;
浮动y;
浮动z;
运算符常量std::数组()常量
{
返回{x,y,z};
}
};
int main()
{
顶点v(0.0f,1.0f,0.0f);
std::数组arr=v;
}
您有很多选择,您选择的内容在很大程度上取决于上下文。以下是“转换”顶点的四种不同方法:
class Vertex
{
public:
Vertex(float x = 0,
float y = 0,
float z = 0)
: x(x), y(y), z(z) {}
operator array<float, 3> () const {
return {x, y, z};
}
array<float, 3> array_copy() const {
return {x, y, z};
}
unique_ptr<float[]> c_array_copy() const {
return unique_ptr<float[]>(new float[3]{ x, y, z });
}
void copy_into(float in[3]) const {
in[0] = x;
in[1] = y;
in[2] = z;
}
float x, y, z;
};
类顶点
{
公众:
顶点(浮动x=0,
浮动y=0,
浮动z=0)
:x(x),y(y),z(z){}
运算符数组()常量{
返回{x,y,z};
}
数组\u copy()常量{
返回{x,y,z};
}
唯一\u ptr c_数组\u copy()常量{
返回唯一的_ptr(新浮点[3]{x,y,z});
}
无效复制到(浮动在[3])常量中{
in[0]=x;
in[1]=y;
in[2]=z;
}
浮动x,y,z;
};
首先,您可以使用()运算符强制转换该类:
cout << "Direct access" << endl;
auto as_array = (array<float, 3>)vertex;
cout << as_array[0] << as_array[1] << as_array[2] << endl;
cout你有很多选择,你选择什么取决于上下文。以下是“转换”顶点的四种不同方法:
class Vertex
{
public:
Vertex(float x = 0,
float y = 0,
float z = 0)
: x(x), y(y), z(z) {}
operator array<float, 3> () const {
return {x, y, z};
}
array<float, 3> array_copy() const {
return {x, y, z};
}
unique_ptr<float[]> c_array_copy() const {
return unique_ptr<float[]>(new float[3]{ x, y, z });
}
void copy_into(float in[3]) const {
in[0] = x;
in[1] = y;
in[2] = z;
}
float x, y, z;
};
类顶点
{
公众:
顶点(浮动x=0,
浮动y=0,
浮动z=0)
:x(x),y(y),z(z){}
运算符数组()常量{
返回{x,y,z};
}
数组\u copy()常量{
返回{x,y,z};
}
唯一\u ptr c_数组\u copy()常量{
返回唯一的_ptr(新浮点[3]{x,y,z});
}
无效复制到(浮动在[3])常量中{
in[0]=x;
in[1]=y;
in[2]=z;
}
浮动x,y,z;
};
首先,您可以使用()运算符强制转换该类:
cout << "Direct access" << endl;
auto as_array = (array<float, 3>)vertex;
cout << as_array[0] << as_array[1] << as_array[2] << endl;
您不能添加转换运算符吗@亚历克斯:我不同意这个骗局。它没有解释如何初始化数组类型。您必须使用原始数组还是可以使用std::array
?@NathanOliver我在问题中没有提到这一点。现在添加了一些背景信息…您是否尝试添加转换运算符@亚历克斯:我不同意这个骗局。它没有解释如何初始化数组类型。您必须使用原始数组还是可以使用std::array
?@NathanOliver我在问题中没有提到这一点。现在添加了一些背景信息…注意,fillArray(float-arr[3])
在编译时无法检测到用户传递的数组太小,这有点危险-它与fillArray(float-arr[])
相同,与fillArray(float*arr
)相同。注意fillArray(float-arr[3])
在编译时无法检测到用户传递的数组太小,这有点危险-它与fillArray(float arr[])相同
与fillArray(float*arr
)相同。谢谢!我接受了你的答案,因为它包括编译时检查!我接受了你的答案,因为它包括编译时检查