Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

declare_class 在头文件中使用的必要性是什么? #14

Closed
hongshui3000 opened this issue Jul 22, 2020 · 1 comment
Closed

declare_class 在头文件中使用的必要性是什么? #14

hongshui3000 opened this issue Jul 22, 2020 · 1 comment

Comments

@hongshui3000
Copy link

hongshui3000 commented Jul 22, 2020

我通过宏展开看到def_class 同样会有类的声明 ,即使不是用declare_class 貌似也不会有什么问题。 我的理解是否对?

@GorgonMeducer
Copy link
Owner

declare_xxxx/dcl_xxxx 首先是一个结构性的东西,它对应C语言里面的前置声明。很多情况下,缺少了前置声明,几个结构体中存在相互引用的指针就做不到了。举一个最简单的例子:

typedef struct {
    ...
   example_t *ptNext;
} example_t;

这是一个很普通的节点,里面有一个指向自己类型的指针,上述代码是编译不过的,因为定义指针的时候,example_t 还不存在——为了解决这个问题就需要前置声明:

typedef struct example_t example_t

struct example_t {
    ...
    example_t *ptNext;
};

declare_xxxx/def_xxxxx 就是对应前置引用。属于必不可少的。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants