Cpp预定义结构?
Cpp predefined structs?
上一次用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;
}
};
首先,使用最新版本的 C++,您可以像这样简化结构定义:
struct Vector2 {
float x{}, y{};
};
这保证了 x 和 y 被初始化为 0,你不需要像你展示的那样单独的构造函数。然后你可以像这样使用结构:
Vector2 myVec; // .x and .y are set to 0
myVec.x = 1; myVec.y = 2;
您甚至可以使用所谓的“初始化列表”来创建一个结构,其中包含 x 和 y 的预定义值而不是默认值 0,如下所示:
Vector2 myVec2{1,2};
关于您对 Vector2 结构的“全局”实例的需求,您可以像在 C# 中一样在 C++ 中使用 static
关键字:
struct Vector2 {
float x{}, y{};
static const Vector2 Zero;
static const Vector2 Unit;
static const Vector2 UnitX;
static const Vector2 UnitY;
};
与 C++ 相比,您不能直接在 class 中指定常量的值(如果您尝试会得到 incomplete type
错误,因为在编译器遇到常量,class定义还未完成);您需要在 class:
之外的某处“定义”常量
const Vector2 Vector2::Zero{};
const Vector2 Vector2::Unit{1.0f, 1.0f};
const Vector2 Vector2::UnitX{1.0f, 0.0f};
const Vector2 Vector2::UnitY{0.0f, 1.0f};
虽然上面的 struct Vector2 ...
通常位于 .h 文件中的某处以被多个其他文件包含,但定义应该放在 .cpp 文件中,而不是 header 中,否则你会得到多重定义的符号错误。
我可能有解决方案,但我不确定。
Vector2.h
struct Vector2 {
static const Vector2 Zero;
static const Vector2 One;
float x {}, y {};
}
Vector2.cpp
#include "Vector2.h"
const Vector2 Vector2::Zero = Vector2 {0, 0};
const Vector2 Vector2::One = Vector2 {1, 1};
上一次用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;
}
};
首先,使用最新版本的 C++,您可以像这样简化结构定义:
struct Vector2 {
float x{}, y{};
};
这保证了 x 和 y 被初始化为 0,你不需要像你展示的那样单独的构造函数。然后你可以像这样使用结构:
Vector2 myVec; // .x and .y are set to 0
myVec.x = 1; myVec.y = 2;
您甚至可以使用所谓的“初始化列表”来创建一个结构,其中包含 x 和 y 的预定义值而不是默认值 0,如下所示:
Vector2 myVec2{1,2};
关于您对 Vector2 结构的“全局”实例的需求,您可以像在 C# 中一样在 C++ 中使用 static
关键字:
struct Vector2 {
float x{}, y{};
static const Vector2 Zero;
static const Vector2 Unit;
static const Vector2 UnitX;
static const Vector2 UnitY;
};
与 C++ 相比,您不能直接在 class 中指定常量的值(如果您尝试会得到 incomplete type
错误,因为在编译器遇到常量,class定义还未完成);您需要在 class:
const Vector2 Vector2::Zero{};
const Vector2 Vector2::Unit{1.0f, 1.0f};
const Vector2 Vector2::UnitX{1.0f, 0.0f};
const Vector2 Vector2::UnitY{0.0f, 1.0f};
虽然上面的 struct Vector2 ...
通常位于 .h 文件中的某处以被多个其他文件包含,但定义应该放在 .cpp 文件中,而不是 header 中,否则你会得到多重定义的符号错误。
我可能有解决方案,但我不确定。
Vector2.h
struct Vector2 {
static const Vector2 Zero;
static const Vector2 One;
float x {}, y {};
}
Vector2.cpp
#include "Vector2.h"
const Vector2 Vector2::Zero = Vector2 {0, 0};
const Vector2 Vector2::One = Vector2 {1, 1};