声明struct在其他c和c ++文件中使用的最佳方法 [英] best way to declare struct to be used in other c and c++ files

查看:64
本文介绍了声明struct在其他c和c ++文件中使用的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我想知道在

其他c和c ++文件中使用结构的最佳方法是什么。例如,对于一个将由其他人使用的C API。


1.在头文件中声明typedef和struct并且

包含所有需要它的源文件中的此文件?例如:


mystruct.h


typedef mystruct mystruct_t;


struct mystruct {

int x;

char c;

};


-OR-

2.在头文件中声明typedef并在

中声明结构.c文件(我不确定这是否有效)。例如:


mystruct.h


typedef mystruct mystruct_t;

mystruct.c
< br $>
#include" mystruct.h"

struct mystruct {

int x;

char c;

};


......(使用该结构的其他代码)


- 或者 -


3.其他方法??


提前感谢您的帮助,

Tammy


解决方案

Tammy写道:


Hello all,


我想知道在

其他c和c ++文件中声明一个结构的最佳方法是什么。例如,对于一个将由其他人使用的C API。


1.在头文件中声明typedef和struct并且

包含所有需要它的源文件中的此文件?



我在头文件中定义了typedef

并将此文件包含在需要它的所有源文件中。

http://www.mindspring.com/~ pfilandr / ... ver / e_driver.h


#define E_TYPE struct {char array [40]; d_type data;} e_type

#define D_TYPE long double d_type

typedef D_TYPE;

typedef E_TYPE;

< a rel =nofollowhref =http://www.mindspring.com/~pfilandr/C/e_driver/target =_ blank> http://www.mindspring.com/~pfilandr/C/e_driver/


-

pete


Tammy写道:
< blockquote class =post_quotes>
Hello all,


我想知道在
中声明一个结构的最佳方法是什么其他c和c ++文件。例如,对于一个将由其他人使用的C API。


1.在头文件中声明typedef和struct并且

包含所有需要它的源文件中的此文件?例如:


mystruct.h


typedef mystruct mystruct_t;


struct mystruct {

int x;

char c;

};



这是公共结构声明的正常方式,

添加有条件编译的externC包装器的好处

包含标题的C ++文件。


-OR-

2.声明头文件中的typedef并在.c文件中声明结构中的结构(我不确定这是否有效)。例如:


mystruct.h


typedef mystruct mystruct_t;


mystruct.c


#include" mystruct.h"

struct mystruct {

int x;

char c;

};



如果你想让mystruct的细节保持隐藏,这也是

常见做法。


-

Ian Collins。


Tammy< Ta*************@gmail.comwrites:


我想知道声明要使用的结构的最佳方法是什么在

其他c和c ++文件中。例如,对于一个将由其他人使用的C API。


1.在头文件中声明typedef和struct并且

包含所有需要它的源文件中的此文件?例如:


mystruct.h


typedef mystruct mystruct_t;


struct mystruct {

int x;

char c;

};



这需要是typedef struct mystruct mystruct_t;


-OR-

2.在头文件中声明typedef并在

中声明结构.c文件(我不确定这是否有效)。例如:


mystruct.h


typedef mystruct mystruct_t;


mystruct.c


#include" mystruct.h"

struct mystruct {

int x;

char c;

};


.....(使用该结构的其他代码)



[...]


这取决于客户端代码是否需要查看成员和/或

声明对象的类型。


使用第二种方法struct mystruct,因此mystruct_t,

就像#include" mystruct.h& ;

关注。这意味着客户端代码可以声明和操作

指向该类型的指针,但它不能声明该类型的对象或

引用其成员。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

诺基亚

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长


Hello all,

I am wondering what is the best way to declare a struct to be used in
other c and c++ files. Such as for a C API that will be used by
others.

1. Declaring the typedef and the struct in the header file and
including this file in all source files that need it? For example:

mystruct.h

typedef mystruct mystruct_t;

struct mystruct {
int x;
char c;
};

-OR-
2. Declaring the typedef in a header file and declaring the struct in
the .c file ( I am not certain that this will work). For example:

mystruct.h

typedef mystruct mystruct_t;
mystruct.c

#include "mystruct.h"

struct mystruct {
int x;
char c;
};

......(other code using the structure)

-OR-

3. Other methods??

Thanks in advance for your help,
Tammy


解决方案

Tammy wrote:

Hello all,

I am wondering what is the best way to declare a struct to be used in
other c and c++ files. Such as for a C API that will be used by
others.

1. Declaring the typedef and the struct in the header file and
including this file in all source files that need it?

I define the typedef in the header file
and include this file in all source files that need it.

http://www.mindspring.com/~pfilandr/...ver/e_driver.h

#define E_TYPE struct {char array[40]; d_type data;} e_type
#define D_TYPE long double d_type
typedef D_TYPE;
typedef E_TYPE;

http://www.mindspring.com/~pfilandr/C/e_driver/

--
pete


Tammy wrote:

Hello all,

I am wondering what is the best way to declare a struct to be used in
other c and c++ files. Such as for a C API that will be used by
others.

1. Declaring the typedef and the struct in the header file and
including this file in all source files that need it? For example:

mystruct.h

typedef mystruct mystruct_t;

struct mystruct {
int x;
char c;
};

This is the normal way for a public structure declaration, with the
addition of a conditionally compiled extern "C" wrapper for the benefit
of C++ files that include the header.

-OR-
2. Declaring the typedef in a header file and declaring the struct in
the .c file ( I am not certain that this will work). For example:

mystruct.h

typedef mystruct mystruct_t;
mystruct.c

#include "mystruct.h"

struct mystruct {
int x;
char c;
};

If you want the details of mystruct to remain hidden, this is also
common practice.

--
Ian Collins.


Tammy <Ta*************@gmail.comwrites:

I am wondering what is the best way to declare a struct to be used in
other c and c++ files. Such as for a C API that will be used by
others.

1. Declaring the typedef and the struct in the header file and
including this file in all source files that need it? For example:

mystruct.h

typedef mystruct mystruct_t;

struct mystruct {
int x;
char c;
};

That needs to be "typedef struct mystruct mystruct_t;".

-OR-
2. Declaring the typedef in a header file and declaring the struct in
the .c file ( I am not certain that this will work). For example:

mystruct.h

typedef mystruct mystruct_t;
mystruct.c

#include "mystruct.h"

struct mystruct {
int x;
char c;
};

.....(other code using the structure)

[...]

It depends on whether the client code needs to see the members and/or
declare objects of the type.

With the second approach struct mystruct, and therefore mystruct_t, is
an incomplete type as far as anything with a #include "mystruct.h" is
concerned. That means that client code can declare and manipulate
pointers to the type, but it can''t declare objects of the type or
refer to its members.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


这篇关于声明struct在其他c和c ++文件中使用的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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