[英]struct keyword in C++ typedef
I was looking at Glew's header and I ran across something that raised a question for me: 我看着格列(Glew)的标题,遇到了一个对我提出疑问的东西:
// C
typedef struct __GLsync *GLsync;
I understand that Glew is a C library, therefore different from C++ and I know that this is a valid code in C++: 我知道Glew是一个C库,因此不同于C ++,并且我知道这是C ++中的有效代码:
// C++
struct S { ... }; // S defined in the class name space
void f( S a ); // struct is optional
My question is, is it safe to remove the struct keyword from the typedef
in C++ and include the header in a C++ only project? 我的问题是,从C ++的
typedef
中删除struct关键字并将其包含在仅C ++的项目中是否安全?
typedef /*struct*/ __GLsync *GLsync;
If not, what would be the difference when I do a typedef
with struct
keyword and without it? 如果没有,当我使用
struct
关键字而不使用typedef
有什么区别?
A typedef of the following form: 以下形式的typedef:
typedef struct S *SPtr;
does not need prior knowledge of what S
is in the same translation unit. 不需要先验知识相同翻译单元中的
S
It assumes a forward declaration of some structure S
and aliases SPtr
as a pointer type to said-same. 它假定某些结构
S
的前向声明和别名SPtr
作为指向相同的指针类型。 However, this: 但是,这:
typedef S *SPtr; // error unless S is declared earlier
requires the declaration of the type S
(which may be a structure) is already known prior to the declaration of the pointer alias type. 需要在声明指针别名类型之前就已经知道类型
S
(可能是结构)的声明。
While this may seem trivial, it is a cornerstone of common implementations of the pimpl idiom . 尽管这看似微不足道,但它是pimpl惯用语的常见实现的基石。 Note the
typedef
isn't really involved here, the announcement of struct MyClassImpl*
as a pointer type to an unknown "thing" to-be-determined later (but prior to actual dereference) is what is important: 请注意,这里没有真正涉及到
typedef
,将struct MyClassImpl*
为指向稍后(但在实际取消引用之前)要确定的未知“事物”的指针类型的声明很重要:
MyClass.h MyClass.h
#ifndef MY_CLASS
#define MY_CLASS
class MyClass
{
... members ...
private:
struct MyClassImpl * m_pimpl; // note no MyClassImpl yet known
};
#endif
MyClass.cpp MyClass.cpp
#include "MyClass.h"
// implementation formal declaration and implementation.
struct MyClassImpl
{
... members...
};
// MyClass implementation using the now-known MyClassImpl
Had that pointer been declared as: 该指针是否声明为:
MyClassImpl * m_pimpl;
the type MyClassImpl
would require prior declaration or a compile-time error would ensue. MyClassImpl
类型需要事先声明,否则会发生编译时错误。
I hope that helps identify a key difference between the two methods of declaration. 我希望这有助于确定两种声明方法之间的关键区别。 (and yes, I know, I should have used a smart pointer =P)
(是的,我知道,我应该使用智能指针= P)
Best of luck. 祝你好运。
The real answer here is maybe . 真正的答案也许是 。 It depends on what other code is found before it.
这取决于之前找到的其他代码。
That being said, you can absolutely change this: 话虽这么说,您可以完全更改此:
typedef struct __GLsync *GLsync;
To this: 对此:
struct __GLsync;
typedef __GLsync *GLsync;
If you want to remove struct
keyword from typedef
then you have to do things like this: 如果要从
typedef
删除struct
关键字,则必须执行以下操作:
typedef struct S { ... }__GLsync; // S defined in the class name space
then you can use: 那么您可以使用:
typedef __GLsync *GLsync;
Appended Question: 附加问题:
If struct
is already definded by already some else then how to do work around then 如果
struct
已经由其他人定义,那么如何解决
typedef struct __GLsync *abc;
typdef *abc GLsync
You can do things like this 你可以做这样的事情
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.