C++ header file (urgent!)

2008-02-14 3:36 am
As I know, functions prototype can be included in header file in reference. But can anyone explain it clearly?

回答 (1)

2008-02-14 6:03 am
✔ 最佳答案
You have raised questions on many topics, namely
1. What are the uses of headers.
2. How to use global variables in functions defined in a different translation unit (file).

1. Headers
In C and C++, if you use a function which has not been defined physically before the usage, the parameters must have been defined so that the compiler can verify the position and type of the parameters used. This comes from the old days when compilers make only one pass through the source file, so there is no way to check unless it was defined before. This requirement is especially true if the function is defined outside of the current translation unit (file), as is the case of library functions such as sqrt(double).
To simplify the task of the programmer, all library functions parameter definitions have been collected in files called header files, which lists the parameter definitions of all functions and constants used in that library. All the programmer has to do is the include that file as if he typed them up just before his program. This reduces clutter and make the user program more readable.

2. Use of global variable outside of the main translation unit.
First off, the use of global variables is discouraged because it contaminates the namespace. You can find surprises when names duplicate in a remote programming unit, and these errors are very hard to trace, and code is hard to maintain.
In fact, object-oriented programming (as in C++) provides tools for encapsulation, enabling free transfer of information, to the right programming unit, without fear of namespace contamination.
Where global variable use is justified, it can be placed outside of all functions.

#include < stdio.h>
int a;
void func1()
{
printf("a=%d\n",a);
}
int main(int argc, char *argv[])
{
a=1;
func1();
}

In the above program, a is printed by the func1 without having to pass the value of a.
This works out very well because func1 and main are placed in the same file. If func1() was defined in a different file, the scope of the global variable a will not extend to different files, and thus will not even compile correctly, since a has never been defined. If you define a as a global variable in a different file, it will NOT work.
#include < stdio.h>
int a; // WRONG, it wont be connected with the other avoid func1()
void funct()
{
printf("a=%d\n",a);
}

The linker can connect the two values during linkage, but needs a little help from the programmer. By putting the keyword extern before the global variable, the linker will not create a new copy of global variable, but will search the value define in another file, and that will enable the value of a to be transmitted as a global variable between files.

#include < stdio.h>
extern int a; // the value of a will be defined in a different translation unit
void func1()
{
printf("a=%d\n",a);
}

Hope this pretty well answers your questions.


收錄日期: 2021-04-13 15:08:58
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20080213000051KK03999

檢視 Wayback Machine 備份