【发布时间】:2022-01-21 10:59:53
【问题描述】:
我上次使用 C++ 是在上大学的时候,所以我不是很流利。
我想创建一个小游戏,因为我习惯了 C#,所以我想创建预定义的结构对象。
这里是 C# 代码供参考:
public struct Vector2 : IEquatable<Vector2>
{
private static readonly Vector2 _zeroVector = new Vector2(0.0f, 0.0f);
private static readonly Vector2 _unitVector = new Vector2(1f, 1f);
private static readonly Vector2 _unitXVector = new Vector2(1f, 0.0f);
private static readonly Vector2 _unitYVector = new Vector2(0.0f, 1f);
[DataMember]
public float X;
[DataMember]
public float Y;
public static Vector2 Zero => Vector2._zeroVector;
public static Vector2 One => Vector2._unitVector;
public static Vector2 UnitX => Vector2._unitXVector;
public static Vector2 UnitY => Vector2._unitYVector;
public Vector2(float x, float y)
{
this.X = x;
this.Y = y;
}
}
在 C# 中,我现在可以使用此代码获取 x = 0 和 y = 0 的向量
var postion = Vector2.Zero;
有没有办法在 C++ 中创建类似的东西,还是我必须接受 c++ 的基本知识并使用这样的结构?
struct Vector2 {
float x, y;
Vector2(float x, float y) {
this->x = x;
this->y = y;
}
};
【问题讨论】:
-
添加
static const Vector2 Zero;对您不起作用?在类中添加static consteval Vector2 Zero();函数怎么样? -
天啊,好像我的大脑暂时被关闭了,。非常感谢,很抱歉问了一个愚蠢的问题