Correct use # include and pre-declaration (forward declaration)

Source: Internet
Author: User

Correct use # include and pre-declaration (forward declaration) 

When I add a class to the project, and that class is related to the class in the project, such as the member variable of that type. The situation is as follows:

/// //. H // class {.......}; /// // B. h // class B: {.... A member;} The result indicates that A class A cannot be found. The solution is to # include "A. h" in B. h ". But sometimes you don't need to # include "A. h". You just need to add class A; before classB:. More seriously, we should not only # include "A. h", but also class ;. At first, I thought it was okay, because it was always compiled and passed, and it won't make the program bigger, because there is # ifndef... # endif and # pragma once control. Until one time, I needed to put those constants in a file, "const. h ", and then include it to other classes that need it. The result cannot be compiled. (because there are too many files, and every file has to include each other, I am also blinded) until today, I finally found the principle in Objective C ++. I would like to share with you an example of the class structure below. (No matter why I don't add a Woman, why does Man have child? I just give an example to illustrate that there is no gender discrimination. The Code is as follows: /// // Main. h ////////////// # Include  "Stdafx. h" # Include  "Man. h" Int  Main (){ Man m;  Return 0; } /// // Person. h ///////////// # Pragma  Once Class  Person { Public : Person (Void); ~ Person (Void); }; /// // Person. cpp /////////// # Include  "StdAfx. h" # Include  ". Person. h" Person: Person (Void){ } Person ::~ Person (Void){ } //// // Man. h /////////// # Pragma  Once # Include  "Person. h" Class  Man: Public Pe

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.