移至头文件 [英] Move To A Header File

查看:75
本文介绍了移至头文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试清理一些代码,我想知道In是否可以将以下内容移动到头文件中,将其命名为proto.h,然后将该文件放置在我的代码开头,其余标题.我是否需要做其他任何事情来做到这一点?能行吗?

I''m trying to clean-up some code and I''m wondering if In could move the following into a header file, name it as such proto.h, and place the file at the start of my code with the rest of the headers. Do I have to do anything else to do this? Will it work?

typedef struct _IP_HEADER_
{
   BYTE  ver_ihl;        // Version (4 bits) and Internet Header Length (4 bits)
   BYTE  type;           // Type of Service (8 bits)
   WORD  length;         // Total size of packet (header + data)(16 bits)
   WORD  packet_id;      // (16 bits)
   WORD  flags_foff;     // Flags (3 bits) and Fragment Offset (13 bits)
   BYTE  time_to_live;   // (8 bits)
   BYTE  protocol;       // (8 bits)
   WORD  hdr_chksum;     // Header check sum (16 bits)
   DWORD source_ip;      // Source Address (32 bits)
   DWORD destination_ip; // Destination Address (32 bits)
} IPHEADER;

typedef struct _TCP_HEADER_
{
   WORD  source_port;       // (16 bits)
   WORD  destination_port;  // (16 bits)
   DWORD seq_number;        // Sequence Number (32 bits)
   DWORD ack_number;        // Acknowledgment Number (32 bits)
   WORD  info_ctrl;         // Data Offset (4 bits), Reserved (6 bits), Control bits (6 bits)
   WORD  window;            // (16 bits)
   WORD  checksum;          // (16 bits)
   WORD  urgent_pointer;    // (16 bits)
} TCPHEADER;

typedef struct _ICMP_HEADER_
{
   BYTE type;               // (8 bits)  
   BYTE code;               // (8 bits)  
   WORD checksum;           // (16 bits)  
} ICMPHEADER;

typedef struct _UDP_HEADER_
{
   WORD  source_port;       // (16 bits)
   WORD  destination_port;  // (16 bits)
   WORD  length;            // Total size of packet (header + data)(16 bits)
   WORD  checksum;          // (16 bits)
} UDPHEADER;

推荐答案

我认为这样做没有问题.它将起作用.最好将声明移至头文件.还应在开始时添加#pragma once头文件(如果使用的是Visual Studio编译器),否则使用宏,这样头文件仅被包含一次,

I think there is no problem doing that.It will work.Its better moving declarations to header file.Also Add #pragma once at the begining of header file if you are using visual studio compiler, otherwise use macros in such a way that the header file is included only once like this,

#ifndef __PROTO_HEADER__  //__PROTO_HEADER__ Must be unique for your project
#define __PROTO_HEADER__
//Declarations
#endif /* __PROTO_HEADER__ */


那是因为您尚未定义它们的含义. #define(定义)每一个之后,您将其余的行留为空白.

根据您要在32/64位上进行编译的系统而定,每一个都可能需要不同地#define(d).

也许像这样:

#define BYTE unsigned char
#define WORD unsigned short
#define DWORD unsigned int

这是因为只要您#define某些东西,然后再使用它,它就会被您#define(d)所代表的意思所代替.



想象以下情况:

#define FALSE 0
#define TRUE 1


如果再写:"int myStatement = TRUE;"
这等效于编写:"int myStatement = 1;"
That''s because you''ve not defined what they mean. After #define(ing) each of them you''ve left the rest of the line blank.

Depending on the system you''re compiling on i.e 32/64 bits each of these may need to be #define(d) differently.

Perhaps something like:

#define BYTE unsigned char
#define WORD unsigned short
#define DWORD unsigned int

This is because whenever you #define something, then use it later it gets substituted by whatever you''ve #define(d) it to mean.



Imagine the following:

#define FALSE 0
#define TRUE 1


If I then write: "int myStatement = TRUE;"
This is equivalent to writing: "int myStatement = 1;"


如果将某些代码移动到头文件(例如Proto.h),则#include"Proto.h"语句将具有在插入文本的地方具有与插入此文本相同的效果.

您可以在头文件中放入任何您喜欢的内容.试想一下,#include语句在那里扩展了.预处理程序通常可以通过适当的选项开关在包含和宏替换之后向您显示结果源代码.

显然,最常见的用法是存储声明,以便可以在需要它们的任何源中方便地重复它们.

解决方案1和解决方案2一样,您不应该添加原始代码中没有的额外声明,例如那些#define语句,因为这些声明是在其他地方定义的.

由于声明的顺序很重要,因此文件包含的顺序也很重要.您可以自行决定要进行声明的地方.如果将它们向上移动,请考虑一下效果.
If you move some code to a header file, say Proto.h, an #include "Proto.h" statement has exactly the same effect as inserting this text, at the place you include it.

You can put about anything you like in a header file. Just imagine that the #include statement is expanded there. Preprocessors are usually able to show you the resulting source code after inclusion and macro substitution, via a suitable option switch.

Obviously, the most common usage is to store declarations so that they can conveniently be repeated in any source that needs them.

Solution 1 is true, as is Solution 2. You shouldn''t add extra declarations that weren''t there in your original piece of code, like those #define statements, because these are defined elsewhere.

As the order of the declaration matters, so does the order of the file inclusions. It is up to you to find the appropriate place for your declarations. If you move them up, think of the effect.


这篇关于移至头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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