C++_ front-facing statement

Source: Internet
Author: User

Why should I have a forward statement?

eg

-Defines a class A, which uses object B of Class B, and then defines a Class B, which also contains an object A of Class A, as follows:

// a.h   " B.h "  class A  {  ....   Private :      b b;  };   // b.h   " a.h "  class B  {  ....   Private :      A;  

A compilation, there is a problem with each other , the solution is to declare Class B in the a.h file, and then use a pointer to B.

// a.h    // #include "b.h"   class B;    class A   {   ....    Private :   b b;   };    // b.h    " a.h "   class B  {   ....    Private :   A;   };

Benefits of the predecessor statement:

-When we use the predecessor Declaration of Class B in Class A, when we modify Class B, we only need to recompile class B, without recompiling A.H's

-Of course, when you really use Class B, you must include the B.h

-Therefore, when you define a header file, you can rely on the predecessor declaration to not rely on the definition, and, where appropriate, should use a type of pre-declaration to reduce the header file import.

eg

-When a pointer member of a class is defined in a header file, the pointer is a pointer to another type class account that is defined in the project account* M_account;

-then add the preceding declaration before the class declaration of the header file: class account;

-OK, without having to include the header file where the account is located

Conditions of use of the predecessor declaration:

-assuming class xxx is in Xxx.h, the following three scenarios can be used with the predecessor declaration without the inclusion of xxx.h

-1. A function declaration with a parameter or return type of XXX;

-2. Class static members of type XXXT;

-3. Class member variable declared as xxx* or xxx&;

-Need to include xxx.h in several cases

-1. If there is a non-static member in your class that defines type XXX

-2. Your class inherits XXX's words

Attention:

-Just about to understand, there are a lot of unclear places

-To be Continued ...

Reference:

-http://blog.csdn.net/moxiaomomo/article/details/8083199

-http://qimo601.iteye.com/blog/1406992

C++_ front-facing statement

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.