[英]understanding the correct usage of pointers
i'm declaring two property inside my interface both of them should be pointers, but xcode gives me two different errors.. 我在接口中声明两个属性,它们两个都应该是指针,但是xcode给了我两个不同的错误。
// myClass.h
#import <Foundation/Foundation.h>
@class CCNode;
@interface myClass : NSObject
{
NSMutableArray myArray;
CCNode myNode;
}
for the NSMutableArray: 对于NSMutableArray:
Interface type cannot be statically allocated
for the CCNode: 对于CCNode:
Field has incomplete type 'CCNode'
in both cases, using pointers will solve the issue, but what's the difference between them? 在这两种情况下,使用指针都可以解决问题,但是它们之间有什么区别?
with try-and-error approach, i found out that if i change @class CCNode
to #import "CCNode.h"
then it gives me the same error as the first line, but i'm definetly missing something for the correct understanding.... 通过@class CCNode
方法,我发现如果我将@class CCNode
更改为#import "CCNode.h"
那么它会给我与第一行相同的错误,但是我肯定会缺少一些正确的理解。 ...
what's the difference between them? 它们之间有什么区别?
The compiler knows the full definition of NSMutableArray
because its header file is included via the Foundation.h
header. 编译器知道NSMutableArray
的完整定义,因为它的头文件是通过Foundation.h
头包含的。 All it knows about CCNode
is that it is an Objective-C class (thanks to the @class
), not how big it is or anything else. 它对CCNode
所了解的CCNode
它是一个Objective-C类(由于@class
),而不是它的大小或其他任何东西。
This is why including CCNode.h
has the effect of changing the error, because the compiler now knows how big it is. 这就是为什么包含CCNode.h
可以更改错误的原因,因为编译器现在知道错误的大小。
Pointers need to be declared with a *
, so your declarations should look like this: 指针需要用*
声明,因此您的声明应如下所示:
@class CCNode;
@interface myClass : NSObject
{
NSMutableArray *myArray;
CCNode *myNode;
}
@class is a forward declaration of your class. @class是您的班级的前向声明。 It has incomplete type because the compiler doesn't know how large it is, whether it's a struct, an object, a builtin type, etc. When you import the header, the compiler has all the info it needs. 它具有不完整的类型,因为编译器不知道它的大小,无论它是结构,对象,内置类型等。在导入标头时,编译器都会拥有所需的所有信息。
In Objective-C, you can't allocate an object on the stack, so the point is kind of moot. 在Objective-C中,您不能在堆栈上分配对象,因此,这很重要。 Objective-C requires that all objects are dynamically allocated (ie on the heap). Objective-C要求动态分配所有对象(即在堆上)。 The error you're getting indicates that you're trying to create a CCNode object on the stack. 您收到的错误表明您正在尝试在堆栈上创建CCNode对象。 All Objective Class objects are pointer types. 所有Objective Class对象都是指针类型。
Statically allocated variables are used for primitive C types like int or double 静态分配的变量用于基本C类型,例如int或double
int marks=100; 整数= 100;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.