简体   繁体   English

这个C ++宏是什么意思?

[英]What is the meaning of this C++ macro?

I can't figure out what this macro means: 我无法弄清楚这个宏意味着什么:

#define DECLARE_HANDLE(n) typedef struct n##__{int i;}*n

DECLARE_HANDLE(HWND);

I have learned from the C program that: 我从C程序中学到了:

"##" means connect the parameter. “##”表示连接参数。

so the macro equals: 所以宏等于:

typedef struct HWND__{int i;}*HWND

Is this right? 这是正确的吗?

If it is right, what is the meaning of that sentence? 如果是对的,那句话的含义是什么?

================== ==================

Code from a game Bombermaaan (for Windows and Linux), 游戏Bombermaaan的代码(适用于Windows和Linux),
link http://sourceforge.net/p/bombermaaan/code/HEAD/tree/trunk/src/Bombermaaan/winreplace.h , 链接http://sourceforge.net/p/bombermaaan/code/HEAD/tree/trunk/src/Bombermaaan/winreplace.h ,
line No 90. 第90行。

The main purpose of this construct is to prevent the misuse of handles. 这种结构的主要目的是防止误操作句柄。 If all handles are simply void * or int or long long or some other basic type, there is nothing to prevent you from using one instead of another. 如果所有句柄都是void *intlong long或其他基本类型,则没有什么可以阻止您使用一个而不是另一个。 A pointer to a struct HWND__ and pointer to struct HBITMAP__ isn't the same thing, so if you have a the following code: 一个指向struct HWND__和指针struct HBITMAP__是不一样的东西,所以如果你有以下代码:

HWND hwnd;
HBITMAP hbmp;

hbmp = GetBitmap(...);
hwnd = hbmp;    // gives compiler error. 

It's a fairly classic technique to ensure that you get unique types for something that the API supplier don't want to provide the true declaration for. 这是一种相当经典的技术,可确保您获得API供应商不希望提供真实声明的特殊类型。 Although I'm not entirely sure why they even need a proper struct declaration, you could probably get away with: 虽然我不完全确定为什么他们甚至需要一个正确的结构声明,你可能会逃脱:

#define DECLARE_HANDLE(n) struct n ## __; struct n ## __ *n;

That will also ensure that any dereferece HWND won't be possible, since the compiler will object to "use of incomplete type". 这也将确保任何解除引用的HWND都是不可能的,因为编译器将反对“使用不完整类型”。

Your assumption is correct. 你的假设是正确的。

You can check this with the following simple test code. 您可以使用以下简单的测试代码进行检查。

DECLARE_HANDLE(HWND);
struct HWND__ s;
HWND p = (HWND) malloc(sizeof(struct HWND__));
s.i = 20;
p->i = 100;
cout << "i valueis " << s.i<<" and " << p->i <<endl;

Exactly. 究竟。

This is used to declare an opaque pointer to a structure that is unknown to you. 这用于声明指向您不知道的结构的不透明指针。

I don't know why they didn't simply declare it as a 我不知道他们为什么不简单地宣布它为一个

typedef void* HWND;

Probably for alignment issues, since structures can be aligned but not basic types . 可能是对齐问题,因为结构可以对齐但不是基本类型
As mentioned above, declaring the type as a structure permits some compile-time type checking. 如上所述,将类型声明为结构允许进行一些编译时类型检查。
Clever! 聪明!

It defines a HWND as a pointer to struct HWND__ that contains int i . 它将HWND定义为包含int i struct HWND__的指针。

So, now you can use in the code the type HWND . 所以,现在你可以在代码中使用类型HWND

DECLARE_HANDLE(HWND); indeed expands to 确实扩展到了

typedef struct HWND__{int i;}*HWND;

Now, you're asking what it means? 现在,你在问这意味着什么? Just split the typedef into two steps: 只需将typedef拆分为两个步骤:

struct HWND__
{
    int i;
};

typedef HWND__* HWND;  // C++ only; for both C++ and C: typedef struct HWND__* HWND;

Thus, it defines a struct HWND__ containing an int (named i ) and declares a type alias HWND as pointer to HWND__ . 因此,它定义了一个包含int (名为i )的结构HWND__ ,并将类型别名HWND声明为指向HWND__指针

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM