gpt4 book ai didi

c++ - Objective-C 与 C++ 中的 Typedef 要求

转载 作者:行者123 更新时间:2023-11-28 03:28:03 25 4
gpt4 key购买 nike

根据我的阅读,我了解到您不需要像在 Objective-C 中那样在 C++ 中使用 typedef。例如,在 C++ 中,您可以这样做:

enum Order {first,second,third};
Order myOrder;
myOrder=first;

但在 Objective-C 中,Order myOrder; 行首先需要一个 typedef:

typedef enum {first,second,third} Order; //noting also the placement of Order at the end
Order myOrder;
myOrder=first;

对吗?如果没有 Objective-C 中的 typedef,您必须通过重复 enum 来定义 myOrder:

enum Order {first,second,third};
enum Order myOrder;
myOrder=first;

如果事实上这一切都是正确的,我觉得这两种语言中存在这种语法差异有点奇怪,因为它们都是基于 C 的,而且这些都不是语言的面向对象性质所独有的, 因此我认为应该是直 C。

最佳答案

其实一点也不奇怪,Objective-C 是 C 的超集,在 C 中你必须再次使用关键字 enum(至于关键字 struct)所以这是连贯的。

如果您希望将敏捷版本切换到 Objective-C++,作为 C++ 的超集,可以使用更简洁的版本,而无需 typedef

实际上 Objective-C 不是基于 C,它是 C + Objective-C,所以它是它的真正扩展。

关于c++ - Objective-C 与 C++ 中的 Typedef 要求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13409950/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com