Header file Declaration

Source: Internet
Author: User
I. Question: Class B; Class A {public: B;}; Class B {public: A;}; The above writing method will cause the header file to contain each other, which is wrong. 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. 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? There is a sequential problem-> 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 classb is declared in advance, the compiler knows that B is a class, but the data in it is unknown, so the size of B is unknown. In this case, compilation fails. (2) pre-declaration: CPP Code // file. code # pragma once # include "B. H "Class A {public: B * B;}; // file B. code # pragma once # include ". H "Class B {public: A * A;}; here both Use Pointer members, so nesting itself will not be a problem. Use # include". H ", the main compilation error is as follows: Error c2501: 'A': Missing storage-class or typespecifiers 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: CPP Code // file. code # pragma once # include "B. H "Class B; // pre-declare Class A {public: B * B;}; // file B. code # pragma once # include ". H "Class B; Class B {public: A * A;}; [visible from top]: header file inclusion cannot replace the pre-declaration. In addition, you can remove. H and B. # include rows in H. iii. Two-point Principle header file inclusion is actually a very cumbersome task, not only do we look tired, the compiler compilation is also very tired, 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. Here, we only propose two principles for the structure of the sentence between different header files and source files. For reference only: the first principle. If the header file can be excluded, do not include it. At this time, the pre-declaration can solve the problem. If only a class pointer or reference is used, and the specific object (non-pointer) of the class is not used, or the specific member of the class is not accessed, then the pre-declaration is enough. Because the size of the pointer data type is specific, the compiler can understand it. The second principle is 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 compiled successfully, 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 ). (From the above text, we can see that if Class A wants to use a specific member of Class B, it needs to use the header file instead of the pre-declaration)

 

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.