本文介绍了一个基本的C问题,循环依赖中的声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
I was doing COM in plain C[^] tutorial. I have the following header file:
#ifndef EXAMPLE_H
#define EXAMPLE_H
#define BUF_SIZE 80
typedef long SetStrPtr(IExample *, char *);
typedef long GetStrPtr(IExample *, char *, long);
typedef struct {
SetStrPtr * SetString;
GetStrPtr * GetString;
} IExampleVtbl;
typedef struct {
IExampleVtbl * lpVtbl;
DWORD count;
char buffer[BUF_SIZE];
} IExample;
#endif
现在,这给了我一个编译错误.因为..
1.声明结构IExample
,我需要先声明IExampleVtbl
2. ..需要声明SetStrPtr
和GetStrPtr
3. ..需要声明IExample
如您所见,这是一个循环依赖性.
Now this gives me a compile error. Because..
1. to declare the structure IExample
, I need to first declare IExampleVtbl
2. ..which needs the declaration of SetStrPtr
and GetStrPtr
3. ..which needs the declaration of IExample
Now as you can see, it''s a cyclic dependency. How to get through this?
推荐答案
#ifndef EXAMPLE_H
#define EXAMPLE_H
#define BUF_SIZE 80
struct _IExample;
typedef struct _IExample IExample;
typedef long SetStrPtr(IExample *, char *);
typedef long GetStrPtr(IExample *, char *, long);
typedef struct {
SetStrPtr * SetString;
GetStrPtr * GetString;
} IExampleVtbl;
struct _IExample{
IExampleVtbl * lpVtbl;
DWORD count;
char buffer[BUF_SIZE];
};
#endif
typedef long SetStrPtr(void *, char *);
typedef long GetStrPtr(void *, char *, long);
因为所有指针的长度都是相同的,所以它会完美地工作,但是不会被强类型化.
It will work perfectly happily because all pointers are the same length, but it won''t be strongly typed.
这篇关于一个基本的C问题,循环依赖中的声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!