VC + + Development of the perfect solution to the problem of the first file of each other resolution _c language

Source: Internet
Author: User

A forward reference is a type that is used to define a variable and declare a function before it is defined.

In general, C + + requires that all types must be defined before use, but in some special cases this requirement is not met, for example, a modeless dialog object pointer is retained in class CMyView, which is used to display/modify some information. To implement the dialog "Apply" button, change the dialog box to the view interface immediately, so you need to save a pointer to the view class in the dialog class, so the definition relationship becomes the following code:

Copy Code code as follows:

#ifndef __myview_h__
#define __myview_h__
This is the header function of the view class
#include "MyDialog.h"
Class CMyView::p ublic CView
{
Protected
CMyDialog * PDLG;
Here are the other definitions
};
#endif

#ifndef __mydialog_h__
#define __mydialog_h__
This is the definition of the dialog box class
#include "MyView.h"
Class CMyDialog::p ublic CDialog
{
Protected
CMyView * PView;
Other definitions
};
#endif

From the compiler's point of view, when compiling MyDialog.CPP, the system first defines the macro __mydialog_h__ and then contains the #include "MyDialog.h" in Myview.h,myview.h because __mydialog_h__ is already defined, So it doesn't work anymore. In the declaration of the CMyView class, cmydialog* Pdlg will cause the compiler to produce errors such as "CMyDialog" types that are not defined, and errors in compiling MyView.CPP files can be similar.

In general, Class A and Class B need to refer to each other, so that a class is defined first, and another class is defined, so that when the class that is defined is referenced by the defined class, it leads to what is called a forward reference.

There are several ways to deal with errors caused by leading references:

1 Use class declaration
Before you refer to a class in advance, you first use a special statement to indicate that the identifier is a class name and is about to be referenced in advance. It is used in the following ways:
A CLASSB class name with class;
b) Define class ClassA
c) Define class CLASSB;
d) The implementation code for two classes is compiled.
The above method applies to all code in the same file, in general, ClassA and CLASSB respectively have their own header files and CPP files, which

Methods need to evolve into:
A) define ClassA and CLASSB separately and implement them in CPP files
(b) Class ClassB and class ClassA at the beginning of the two header file;
c) contains a header file for another class in two CPP files
Note: This method does not use the class name to define variable arguments for variables and functions, but to define references or pointers.

2) using global variables
Because global variables can avoid advance references, don't repeat them. My habit is to add the class object's extern statement to the end of the class header file, we like how to write that there is no big problem, the key is to ensure that not in the header file included in the random.

3 Use a base class pointer.
The method is to use the base class pointer in reference to the advanced reference class. In general, two classes that refer to each other do not involve their base class, and therefore do not cause a forward reference. To start with the example said: In the CMyDialog class using cview* instead of cmyview*, in the CMyView class with cdialog* instead of cmydialog*, this will not lead to a reference.

Note: in this paper, in order to describe the convenience, the class AClass statement into the AClass declaration, the class AClass start to the aclass of classes member variables, member function prototypes, such as the definition of class, The section in CPP is referred to as the definition of a class. If you have a different understanding of these three words, please change these three words to the corresponding words according to your own intention.

PS: I use the first method to solve the problem.

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.