Some issues related to header files in C ++

Source: Internet
Author: User

I. Questions about class nesting

Repeated inclusion of the C ++ header file is really a headache. This problem occurs more than once when a simple data structure demo program was created some time ago. Suppose we have two classes A and B, which are defined in their respective files. h and B. h, but B and B must also use A in A, but such writing is of course incorrect:
Class B;

Class
{
Public:
B B;
};

Class B
{
Public:
A;
};
It is A logical error and cannot be implemented because A space belongs to B should be opened in object A and B has space for. Here, we only need to change the B type member in one of Class A to the pointer form to avoid this infinite extension of the strange circle. Why do we need to change A instead of B? Because even if you do similar operations in B, it will still cause compilation errors. On the surface, this is only a sequential problem.
Why? Because the C ++ compiler always needs to know the size of the defined data type when compiling source files from top to bottom. After the statement class B is declared in advance, the compiler knows that B is a class, but the data in the statement is unknown, so the size of B is unknown. In this case, compilation fails. The following compilation error occurs in VC ++ 6.0:
Error C2079: 'B' uses undefined class 'B'
After changing B in a to B pointer type, the space occupied by the pointer on A specific platform is certain (4 bytes on Win32 platform), so that compilation can be performed.

Ii. Nesting of classes in different header files

In actual programming, different classes are generally placed in different independent header files, so that the two classes will have different problems when referencing each other. Repeated compilation is the root cause of the problem. To ensure that the header file is compiled only once, a common method in C ++ is to use the Conditional compilation command. In the header file, we often see the following statement segment (taking the header file automatically generated by VC ++ 6.0 as an example ):

# If! Defined (afx_stack_h1_1f725f28_af9e_4beb_8560_67813900ae6b1_included _)
# Define afx_stack_h1_1f725f28_af9e_4beb_8560_67813900ae6b1_included _
// Many statements ......
# Endif

The first sentence # if! Defined is also often used for # ifndef, which works the same. If this macro is not defined, define it and execute all statements until # endif. If you want to execute this code next time, the duplicate code will not be executed again because the macro has been defined. This is a clever and efficient way. In the later version of VC ++, you can also use this command to replace all the above:
# Pragma once
It means that the code in this file is used only once.

But don't think that this mechanism is all done, for example, in the following code:

// Code in file A. h
# Pragma once

# Include "B. h"

Class
{
Public:
B * B;
};

// Code in file B. h
# Pragma once

# Include "A. h"

Class B
{
Public:
A *;
};

Here both Use Pointer members, so there is no problem with nesting itself. After using # include "A. h" in front of the main function, the main compilation error is as follows:
Error C2501: 'A': missing storage-class or type specifiers
It is still a type error that cannot be found. In fact, a pre-declaration is still required here. After adding the pre-declaration, You can compile the statement successfully. The code format is as follows:

// Code in file A. h
# Pragma once

# Include "B. h"

Class B;

Class
{
Public:
B * B;
};

// Code in file B. h
# Pragma once

# Include "A. h"

Class B;

Class B
{
Public:
A *;
};

In this way, the header file cannot replace the pre-declaration. Sometimes the problem can only be solved through pre-declaration. We also need to think about it. Is it necessary to include the header file when there is a pre-declaration? We try to remove the # include row in A. h and B. h and find that no new errors have occurred. So when does a pre-declaration and header file need to be included?

Iii. Two principles

Header file inclusion is actually a very cumbersome task. We are not only tired, but also very tired when compiling the compiler, coupled with the macro definition that often appears in the header file. It seems that the expansion of various macro definitions is very time-consuming and far slower than the expansion of custom functions. I only propose two principles for the structure of the sentence between different header files and source files, for your reference only:

The first principle should be: do not include header files if they are not included. At this time, the pre-declaration can solve the problem. If you only use a class pointer and do not use a specific object (non-pointer) of the class or access a specific member of the class, you can use the pre-declaration. Because the size of the pointer data type is specific, the compiler can understand it.

The second principle should be to include the header file in the CPP file as much as possible, rather than in the header file. Assume that A member of Class A is A pointer to Class B. in the header file of Class A, the pre-declaration of Class B is used and the result is cheap, so in the implementation of A, we need to access the specific members of B, so we need to include the header file, then we should in the implementation part of Class A (CPP file) contains Class B header files rather than declarative parts (H files ).

Related Article

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.