本文介绍了正向申报在Objective-C的一个结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个协议,而参数我定义的方法之一是 CMTime * 。我想申报转发 CMTime ,而不是包括它。不过,我已经试过 @class CMTime 并抱怨说,它是为不同类型的符号在其他地方重新定义。文件说,这是一个结构。我试着向前声明为

I'm creating a protocol, and one of the parameters to a method I'm defining is a CMTime*. I would like to forward declare CMTime as opposed to including it. However, I've tried @class CMTime and it complains that it is redefined elsewhere as a different type of symbol. Documentation says it's a struct. I've tried forward declaring it as

struct CMTime;

但它仍然抱怨它不知道它是什么。

but it still is complaining that it doesn't know what it is.

任何想法我做错了吗?

推荐答案

编译为ObjC源具有相同的规则为C在这方面。

A source compiled as ObjC has the same rules as C in this regard.

编译为ObjC ++源在这方面有相同的规则,C ++。

A source compiled as ObjC++ has the same rules as C++ in this regard.

@class MONClass; 是ObjC类型的向前声明。不要使用它的结构。

@class MONClass; is a forward declaration of an ObjC type. Do not use it for structs.

结构t_mon_struct; 是一个向前声明中的命名的C或C ++结构。不要用它来ObjC类型。从技术上讲,编译器可以让你也宣告着一个C ++类作为一个结构(当然提供的类是在全局命名空间也宣告)。

struct t_mon_struct; is a forward declaration of a named C or C++ struct. Do not use it for ObjC types. Technically, the compiler allows you to also forward declare a C++ class as a struct (provided of course the class is also declared in the global namespace).

因此​​,语义的根源都归结为C(假设这是一个ObjC译)。我会停下来提ObjC和C ++了。

Thus, the root of the semantics all boil down to C (assuming this is an ObjC translation). I'll stop mentioning ObjC and C++ now.

有复杂的一些常见的源位置:

There are some common sources of complexity here:


  • 的结构空间

  • 结构体的声明

  • 标签,避免多次定义

结构t_mon_struct; 是一个标记结构的正向声明。具体来说,就是他的名字在空间结构存在。

struct t_mon_struct; is a forward declaration of a tagged struct. Specifically, that is whose name exists in the struct namespace.

标记的一个结构中存在的结构命名空间:

a tagged struct which exists in the struct namespace:

struct t_mon_struct { int a; };

匿名结构有一个typedef在全局命名空间:

an anonymous struct with a typedef in the global namespace:

typedef struct { int a; } t_mon_struct;

标记一个与结构在全局命名空间的typedef:

a tagged struct with a typedef in the global namespace:

typedef struct t_mon_struct { int a; } t_mon_struct;

CMTime 声明如下:

typedef struct
{
    CMTimeValue    value;
    CMTimeScale    timescale;
    CMTimeFlags    flags;
    CMTimeEpoch    epoch;
} CMTime;

具体而言,全球的typedef标签 CMTime ,势必在结构命名空间中的匿名结构,不得引用,除非它的声明是可见的。

Specifically, the global typedef label CMTime is bound to an anonymous struct in the struct namespace, and may not be referenced unless its declaration is visible.

要是 CMTime 被宣布:

typedef struct CMTime
{
    CMTimeValue    value;
    CMTimeScale    timescale;
    CMTimeFlags    flags;
    CMTimeEpoch    epoch;
} CMTime;

,那么你可以通过使用预先声明结构CMTime 已经得到了:

struct CMTime;
void foo(struct CMTime*);

由于未声明的方式,你需要的#include 的声明,或设计一个解决办法。

Since it wasn't declared that way, you'll need to #include its declaration, or devise a workaround.

当结构体的类型定义是从它的标签不同的并发症恶化。你无法绑定或重新声明一个typedef(C语言)。但是,您可以通过在结构命名空间中的名字周围潜行 - 其中有些图书馆作者认为作为是私人

The complications worsen when the the struct's typedef is distinct from its tag. You can't bind to or redeclare a typedef (in C). However, you can sneak around it by using the name in the struct namespace -- which some library authors consider as being private.

这篇关于正向申报在Objective-C的一个结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 13:46