Objective-C中此数据结构(C++)的替代方案
Alternative for this data structure(C++) in Objective-C
我在C++中有这个数据结构(结构):
struct Vector3f
{
float x;
float y;
float z;
Vector3f()
{
}
Vector3f(float _x, float _y, float _z)
{
x = _x;
y = _y;
z = _z;
}
};
我最近一直在学习和使用Objective-C。 我发现有很多事情我在 Objective-C 中做不到,而我在C++中却能做到。 所以,我希望能够在Objective-C中使用构造函数来做到这一点。 我知道Objective-C不支持像C++那样的函数重载。 因此,不需要第一个构造函数。
您只需使用三个属性:
@interface Vector : NSObject
@property(nonatomic, assign) float x, y, z;
- (id)init;
- (id)initWithX:(float)x y:(float)y z:(float)z;
@end
@implementation Vector
- (id)init {
// Members default to 0 implicitly.
return [super init];
}
- (id)initWithX:(float)x y:(float)y z:(float)z {
if (self = [super init]) {
self.x = x;
self.y = y;
self.z = z;
}
return self;
}
@end
请注意,此处重写init
是可选的,因为它所做的只是调用超类的init
方法。
相关文章:
- 运行同一解决方案的另一个项目的项目
- Project Euler问题4的错误解决方案
- 计算每个节点的树高,帮助我解释这个代码解决方案
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- visual c++,如何获取解决方案目录中的代码
- 有没有办法在远程设备上打开和编辑visual Studio 2017解决方案
- C++Matching Brackets 2解决方案不起作用
- 在 ubuntu3 上C++ goto 定义有什么解决方案吗16.04?
- C++11 中不同类型的对象的 std::array 的替代方案
- 在 leetcode 上提交解决方案时出现堆栈缓冲区溢出错误
- 别名模板的专业化 C++11 中没有开销的最佳替代方案
- 我的固定时间步长与增量时间和插值的解决方案是错误的吗?
- 无法在问题解决方案中执行输出逻辑
- 最大的回文产品 - 程序未运行,编写解决方案但无法理解问题
- 从预序遍历构造 bst 的 c++ 和 python 解决方案之间的区别
- 在一个解决方案中针对第三方静态库 (Creo) 的不同版本(版本)进行构建
- 如何巧妙地编写两个函数——一个用于检查是否存在解决方案,另一个用于获取所有解决方案
- 使用 Git 处理 C++ Visual Studio 2019 解决方案的外部依赖项源代码管理的最佳方法是什么?
- N-queen问题:无法弄清楚为什么我的解决方案不起作用
- 从排序数组中删除重复项,具有不同代码方式的相同解决方案具有不同的输出