简体   繁体   English

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

[英]Typedef requirement in Objective-C vs C++

From my reading, I am gathering that you don't need typedef in C++ in the same way you do in Objective-C. 从我的阅读中,我收集到你不需要在C ++中使用typedef,就像在Objective-C中一样。 For example, in C++ you can do this: 例如,在C ++中,您可以这样做:

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

But in Objective-C, the line Order myOrder; 但是在Objective-C中,行Order myOrder; would first require a typedef : 首先需要一个typedef

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

Correct? 正确? Without the typedef in Objective-C you'd have to define myOrder by repeating the enum : 如果没有Objective-C中的typedef ,你必须通过重复enum来定义myOrder

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

If in fact this is all correct, I find it a bit odd that there is this type of syntactic differences in the languages, since both are based on C and none of this is unique to the object-oriented natures of the languages, thus should be just straight CI would think. 如果事实上这一切都是正确的,我发现在语言中存在这种类型的语法差异有点奇怪,因为两者都基于C并且这些都不是语言的面向对象性质所独有的,因此应该只是笔直的CI会想。

Actually that's not strange at all, Objective-C is a superset of C and in C you must use the keyword enum again (as for the keyword struct ) so that's coherent. 实际上这并不奇怪,Objective-C是C的超集,而在C中你必须再次使用关键字enum (对于关键字struct ),这样就是连贯的。

If you want the agile version switch to Objective-C++ which, being a superset of C++ allows the less verbose version without the requirement of a typedef . 如果您希望敏捷版本切换到Objective-C ++,那么作为C ++的超集允许不那么详细的版本而不需要typedef

Actually Objective-C is not based on C, it is C + Obective-C so it's a real extension to it. 实际上Objective-C不是基于 C,它是C + Obective-C所以它是它的真正扩展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM