Cross-reference methods

Source: Internet
Author: User
Header file inclusion problems when two different classes reference each other 2008-06-02 15:45:36 Tags: How to reference different types of header files [Push to technical circles]

Reprinted from the http://www.cnblogs.com/sunnyjones/archive/2008/03/10/1099521.html the so-called advance Reference refers to a type before the definition is used to define variables and declare functions. In general, C/C ++ requires that all types be defined before use, but in some special cases, such requirements cannot be met, for example, A non-mode dialog box Object Pointer is retained in cmyview, which is used to display/modify some information. To implement the "Apply" button of the dialog box, update the changes made in the dialog box to the View Interface immediately. Therefore, you need to save the View class pointer in the dialog box class, in this way, the relationship is defined as follows: Code : # Ifndef _ myview_h __
# DEFINE _ myview_h __
// This is the header function of the View class.
# Include "mydialog. H"
Class cmyview: Public cview
{
Protected:
Cmydialog * pdlg;
// Other definitions
};
# Endif

# Ifndef _ mydialog_h __
# DEFINE _ mydialog_h __
// This is the definition of the dialog box class
# Include "myview. H"
Class cmydialog: Public cdialog
{
Protected:
Cmyview * pview;
// Other definitions
};
# Endif

From the compiler perspective, compile mydialog. in CPP, the system first defines macro _ mydialog_h __, and then contains myview. h, myview. # include "mydialog. H "since _ mydialog_h _ has been defined, it does not work anymore. In the cmyview class declaration, cmydialog * pdlg; causes the compiler to generate errors such as the "cmydialog" type not defined. The errors in compiling the myview. cpp file can be similar.
Generally, classes A and B need to be referenced from each other, so that one class is first defined, and the other class is then defined, in this way, the class that is first defined after the class is referenced leads to the so-called advance reference. There are several solutions to errors caused by advanced reference:
1) Use class declaration
Before a class is referenced in advance, a special statement is used to indicate that the identifier is a class name and will be referenced in advance. The usage is as follows:
A) use Class B to declare the class name to be referenced in advance
B) define class;
C) define class B;
D) Compile the implementation code of the two classes.
The preceding method applies to all codes in the same file. Generally, classes A and B have their own header files and CPP files respectively.
Methods need to evolve:
A) Define A and B respectively, and implement
B) use class B and class A at the beginning of the two header files to declare the other party.
C) The two CPP files contain the header files of the other class.
Note: Do not use a class name to define variable parameters of variables and functions. You can only define references or pointers.

2) Use global variables
Because global variables can avoid advanced references, you do not need to repeat them. My habit is to use as few global variables as possible, which is confusing. 3) use a base class pointer.
This method uses a base class pointer When referencing a class that is referenced in advance. Generally, two referenced classes do not involve their base classes, so
Advanced reference. In the example below, cview * is used in the cmydialog class to replace cmyview *, and cdialog * is used in the cmyview class to replace cmydialog *.
Will not cause advance reference.

This article is from 51cto. com technical blog

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.