Highlights of c ++ header files (favorites)

Source: Internet
Author: User

The declaration of classes and the definition of classes in C ++ are almost unwritten rules. The benefit of doing so is that the class declaration and implementation are separated, clear, and easy to publish library functions. However, in actual programming, some undefined compilation errors are often caused by the inclusion sequence of header files. It is sometimes a headache if you do not understand the principles. To eliminate compilation errors with undefined symbols, the most basic method is to reference a symbol (including variables, functions, structures, classes, etc) make sure that it has been declared or defined before.

In actual coding design, the most basic principle is that it is best not to include other header files in the Class header files, because this will complicate the file inclusion relationship between classes. To comply with this principle to the maximum extent, the actual coding design process can adopt the following two methods:
 
First, try to maintain the independence of the class when designing a class, even if the class does not depend on other class libraries or function libraries as much as possible, or in a back step, try not to rely on other classes in the class declaration. In this way, no other header files can be included in the declared header files of this class. If other classes are used in the implementation, you can only include the header files of the class library or function library used in the implementation file of this class.
 
Method 2: When other class libraries or function libraries must be used in the class declaration, method 1 is no longer applicable, when a class declaration references pointer references of other classes or structures or function references, the above principles can also be maintained. The approach is to use forward references, and declare the class name or function name used by this class before the declaration of this class. When the class declaration references an instance of another class, the above principle becomes unretained. Only the header file of the referenced class library or function library is referenced in the Declaration header file of this class.
However, in practice, if a class requires many other class pointers, structure pointers, or function names, the above principles can be maintained even though method 2 is used, however, it is troublesome to declare all the classes and methods used before the class. In this case, for convenience, you have to add header files in other class libraries or function libraries to the declared header files of this class.
 
Example: www.2cto.com
Example 1: The simplest case is that there is no relation between two classes A and B.
In this case, the declaration and definition files of the two classes do not need to contain the header file of the other party. (Although it is nonsense, many people's Code still contains a large number of header files in this case. The main reason is: lazy, don't want to think too much)
 
Example 2: Two Classes A and B use each other during implementation
In this case, you only need to include the header file of the used class in the implementation file of each class.
 
Example 3: When two classes A and B are declared, they are referenced by the pointer to the other party.
In this case, you can declare the classes used before the class declaration (header file), and then include the declared header file of the classes used in their respective header files. For example:
// A. h
Class B;
Class
{
B * pb;
}
 
// B. h
Class;
Class B
{
A * pa;
}
 
// A. cpp
# Inclue "B. h"
# Inclue "A. H"
......
 
// B. cpp
# Inclue "A. h"
# Inclue "B. h"
......

In addition, in large-scale engineering program design, a class usually needs to use many existing class libraries and function libraries, adding all the class library header files used by a class to the class definition header file is also very troublesome. At this time, the method is to add those frequently used header files to a public header file, this public header file is called push. h. It should be noted that some header files are also dependent on the system, and the inclusion sequence of these files is also careful, otherwise errors will occur. Ps: the header file contains the sequence from the most special to general. For example, we should use this method # include the header file:
From the most special to the most general, that is,
# Include "This Class header file"
# Include "this directory header file"
# Include "self-written tool header files"
# Include "third-party header files"
# Include "platform-related header files"
# Include "C ++ library header files"
# Include "C library header file"


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.