是类struct或struct指针 [英] Are classes structs or struct pointer
问题描述
如果我没有错误,structs是指对象,struct指针意味着指向对象的指针?在一篇文章中,它说,类是结构体,这意味着它们是对象。
If I'm not mistaken, structs mean objects, and struct pointers mean pointer to objects right? In an article, It says that classes are structs which means that they are objects.
Filter the list of all classes
The traditional definition of a class in Objective-C looks like this:
struct objc_class {
Class isa;
Class super_class;
const char *name;
long version;
long info;
long instance_size;
struct objc_ivar_list *ivars;
struct objc_method_list **methodLists;
struct objc_cache *cache;
struct objc_protocol_list *protocols;
};
因此,如果它说的类是struct,那么它们如何适合 objc_msgSend(id self,SEL_cmd)
这需要一个类型 id
?
So, if it says that classes are structs, then how will they fit in the argument of objc_msgSend(id self, SEL_cmd)
which requires a struct pointer which is of type id
?
推荐答案
类是结构体,但类
是一个指针类型,定义为
Classes are structs, but Class
is a pointer type, being defined as
typedef struct objc_class *Class;
这回答问题的第一部分。
and this answers the first part of the question.
现在,如果你看看< objc / objc.h>
,你会发现
Now, if you take a look at <objc/objc.h>
you'll find
struct objc_class {
Class isa OBJC_ISA_AVAILABILITY;
#if !__OBJC2__
Class super_class OBJC2_UNAVAILABLE;
const char *name OBJC2_UNAVAILABLE;
long version OBJC2_UNAVAILABLE;
long info OBJC2_UNAVAILABLE;
long instance_size OBJC2_UNAVAILABLE;
struct objc_ivar_list *ivars OBJC2_UNAVAILABLE;
struct objc_method_list **methodLists OBJC2_UNAVAILABLE;
struct objc_cache *cache OBJC2_UNAVAILABLE;
struct objc_protocol_list *protocols OBJC2_UNAVAILABLE;
#endif
} OBJC2_UNAVAILABLE;
和< obj / runtime.h>
您会发现
/// Represents an instance of a class.
struct objc_object {
Class isa OBJC_ISA_AVAILABILITY;
};
这意味着在Objective-C 2.0 objc_object
和 objc_class
是相同的结构,都有 isa
字段。
meaning that in Objective-C 2.0 objc_object
and objc_class
are identical structs, both having an isa
field.
这就是为什么你需要传递 Class
,其中需要 id
:类是对象。
That's why you can pass a Class
where an id
is required: classes are objects after all.
你通常期望有关不兼容指针类型的警告,但显然Obj-C编译器使用特殊处理这种特定情况。
You would normally expect a warning about the incompatible pointers type, but apparently the Obj-C compiler uses an ad-hoc treatment this specific case.
I have no references to support this, though.
我最终在clang源代码中找到一个引用:
I finally found a reference in the clang source code:
在 ASTContext.cpp
code> Class 声明
In ASTContext.cpp
, this is the Class
declaration
TypedefDecl *ASTContext::getObjCClassDecl() const {
if (!ObjCClassDecl) {
QualType T = getObjCObjectType(ObjCBuiltinClassTy, 0, 0);
T = getObjCObjectPointerType(T);
TypeSourceInfo *ClassInfo = getTrivialTypeSourceInfo(T);
ObjCClassDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this),
getTranslationUnitDecl(),
SourceLocation(), SourceLocation(),
&Idents.get("Class"), ClassInfo);
}
return ObjCClassDecl;
}
这是 id
声明
TypedefDecl *ASTContext::getObjCIdDecl() const {
if (!ObjCIdDecl) {
QualType T = getObjCObjectType(ObjCBuiltinIdTy, 0, 0);
T = getObjCObjectPointerType(T);
TypeSourceInfo *IdInfo = getTrivialTypeSourceInfo(T);
ObjCIdDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this),
getTranslationUnitDecl(),
SourceLocation(), SourceLocation(),
&Idents.get("id"), IdInfo);
}
return ObjCIdDecl;
}
在这两种情况下,类型都设置为 getObjCObjectPointerType
。这使得编译器考虑 Class
和 id
作为指向Objective-C对象的指针。
In both cases the type is set to the result of getObjCObjectPointerType
. This causes the compiler to consider both Class
and id
as pointers to Objective-C objects.
这篇关于是类struct或struct指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!