需要帮助理解这个基本的 Vector3D 结构的代码
Need help understanding this basic Vector3D structure's code
我试图通过一本书学习线性代数,作者为Vector3D的基本结构留下了一个代码块。除了float&运算符和常量float&操作人员如果我能在这里得到对代码的深入解释,这可能吗?
我曾尝试在谷歌上搜索运算符的使用情况,这使得这两个代码块看起来像是在进行某种隐式转换,但我仍然对其他一切感到不知所措。
#include <iostream>
using namespace std;
//Math Engine
struct Vector3D
{
float x, y, z;
Vector3D(float X, float Y, float Z)
{
x = X;
y = Y;
z = Z;
}
Vector3D() = default;
float& operator [](int i)
{
return ((&x)[i]);
}
const float& operator [](int i) const
{
return ((&x)[i]);
}
};
int main()
{
return 0;
}
#include <iostream>
using namespace std;
//Math Engine
struct Vector3D
{
//take array instead of separate elements, this it will be contiguous in memory
float x[3];
Vector3D(float X, float Y, float Z)
{
x[0] = X;
x[1] = Y;
x[2] = Z;
}
Vector3D() = default;
//this will allow to Get and Set the value of vector
float& operator [](int i)
{
assert( 0 <= i && i < 3 );
return x[i];
}
//this will **NOT** allow to Set the value of vector but you can get it
const float& operator [](int i) const
{
assert( 0 <= i && i < 3 );
return x[i];
}
};
int main()
{
return 0;
}
相关文章:
- 如何循环打印顶点结构
- 通过方法访问结构
- 使用不带参数的函数访问结构元素
- 预处理器:插入结构名称中的前一个行号
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 孤立代码块在结构中引发异常
- 有什么方法可以遍历结构吗
- 如何在 C# 中映射双 C 结构指针?
- 如何在C++中使用结构生成映射
- 无法将结构注册为增强几何体3D点
- 多成员Constexpr结构初始化
- C++将文本文件中的数据读取到结构数组中
- 如何重构类层次结构以避免菱形问题
- 如何在C++中序列化结构数据
- std::vector的包装器,使数组的结构看起来像结构的数组
- 没有为自己的结构调用列表推回方法
- 奇怪的结构&GCC&clang(void*返回类型)
- 在 c++ 中拥有一组结构的正确方法是什么?
- vscode g++链路故障:体系结构x86_64的未定义符号
- 需要帮助理解这个基本的 Vector3D 结构的代码