一个基本的C问题,循环依赖中的声明 [英] A Basic C question, declarations in cyclic dependency

查看:77
本文介绍了一个基本的C问题,循环依赖中的声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在纯C中执行 COM [

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. ..需要声明SetStrPtrGetStrPtr
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?

推荐答案

尝试一下:
try 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


这很讨厌,但是唯一的方法是更改​​IExample指针使指针无效:
It''s nasty, but the only way to do it is you change the IExample pointers to void pointers:
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问题,循环依赖中的声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆