开发者

forward declare typedef'd struct

开发者 https://www.devze.com 2022-12-22 23:03 出处:网络
I can\'t figure out how to forward declare a windows struct. The definition is typedef struct _CONTEXT

I can't figure out how to forward declare a windows struct. The definition is

typedef struct _CONTEXT
{
 ....
} CONTEXT, *PCONTEXT

I really don't want to pull into this header, as it gets included everywhere.

I've tried

stru开发者_StackOverflow社区ct CONTEXT

and

struct _CONTEXT

with no luck (redefinition of basic types with the actuall struct in winnt.h.


extern "C" { typedef struct _CONTEXT CONTEXT, *PCONTEXT; }

You need to declare that _CONTEXT is a struct. And declare it as extern "C" to match the external linkage of windows.h (which is a C header).

However, you don't need to provide a definition for a typedef, but if you do, all definitions must match (the One Definition Rule).

EDIT: I also forgot the extern "C".


Not solution but workaround:

// h-file
struct MyContext; // forward decl
void f(MyContext * pContext); // use pointer


//cpp-file
#include <windows.h>
struct MyContext {
   CONTEXT cont;
};

void f(MyContext * pContext)
{
   CONTEXT * p_win_cont = & pContext->cont;
   // use p_win_cont
   // ....
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号