Differences between. h and. cpp (zt)

Source: Internet
Author: User

As I am new to VC ++ development, I need to master some principles of C ++. In particular, you need to have a deep understanding of the concept of source files and header files:
Differences between header files and source files
First, we can put everything in a. cpp file.
Then the compiler will compile this. cpp into. obj. What is obj?
A program can be composed of a compilation unit or multiple compilation units. If you do not want to make your source code hard to read,
Please use multiple compilation units. (A function cannot be placed in two compilation units, but more than two
You can put them in one unit, that is, cpp)
Program, that is, the program.
What if one. cpp uses a function defined by another. cpp? You only need to write his function declaration in this. cpp type. The other work is done by the linker. You can call this function at will.

The linker connects all obj objects, but what if the same function or external variable happens? How does he identify it?
Generally, two identical function names or external variable names cannot appear in the same program.
Fortunately, c ++ can be limited by a keyword called the link property. Your function belongs to the whole program.
It is only used in a compilation unit obj.
These keywords are extern and static. extern indicates the external link, that is, in addition to this unit, the external unit.
Static is an internal link and belongs to its own unit.
After talking about it for so long, I haven't talked about the role of. h yet?

Actually no. h can also work well, but when you find a function or external variable with an external link, you need many declarations, because c ++ is a language that uses functions and variables, he must be declared. Why? Only after the declaration can we know his specification and better find out what is not the same as the specification. don't think about a compilation unit. It will automatically get information from another compilation unit and know how you define this function.
Therefore, as long as the unit of the function is used, a declaration must be written in that. in cpp, this is not very troublesome, and if you want to modify it, you must modify it one by one. this is really unbearable.

. H was born to solve this problem. He included these public things. then all. cpp, which can be included in # include. you need to modify the content later.
Please be careful not to abuse. h, do not write code in. h. h is not a. cpp repository, and everything is put in it.

If you write code in it. when cpp contains it, it will be repeatedly defined, for example, the function func () {printf}; put it in the header file. h, there are also some. statements required by cpp;

Then you find that B. cpp needs to use a function in a. cpp, so you are very happy to include a. h.
Note: # include is not an application instruction. It just copies the content of the specified file.
In this case, both a. cpp and B. cpp have the definition of a func () function.
If the function is static with internal links, it would be a waste of space;
If it is extern, external links (this is the default situation), it cannot appear in the same program according
If the same name function is required, the connector will show you a connection error without mercy!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.