VC++ development in the perfect solution to the problem of header file containing methods

  • 2020-04-02 01:36:31
  • OfStack

A leading reference is a type that is used to define variables and declare functions before it is defined.

In general, C/C++ requires that all types be defined before they are used, but in some special cases this requirement cannot be met, for example, a pointer to a non-modal dialog object is reserved in the class CMyView to display/modify some information. In order to implement the "apply" button in the dialog box, update the changes made in the dialog box to the view interface immediately. To do this, we need to save the pointer of the view class in the dialog box class, so that the definition of the relationship becomes the following 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;
       //Here's another definition
   };
   #endif

   #ifndef __MYDIALOG_H__
   #define __MYDIALOG_H__
   //This is the definition of the dialog class
   #include "MyView.h"
   class CMyDialog::public CDialog
   {
       protected:
CMyView * pView;
//Other definitions
   };
   #endif

From the compiler's point of view, when compiling mydialog.cpp, the system first defines the macro, s, s, s, s, s, s, s and then includes myview. h. In the declaration of the CMyView class, CMyDialog; This will cause the compiler to produce errors of type "CMyDialog" not defined and so on. The errors in compiling the myview.cpp file can be obtained similarly.      

In general, class A and class B need to refer to each other, so that one class must be defined first, and another class must be defined later, so that when the first class is defined after the first class is defined, this leads to the so-called ahead reference.

There are several ways to deal with errors caused by overquoting:

1) use class declarations
Before referencing a class in advance, a special statement states that the identifier is a class name that will be referenced in advance. The usage method is as follows:
A)   Use the class ClassB; Declares the name of the class to be referenced
B)   Define a class ClassA
C)   Define a class ClassB;
D)   Write the implementation code for the two classes.
The above method applies to all code in the same file, typically ClassA and ClassB have their own header and CPP files, respectively

The method needs to evolve into:
A) define ClassA and ClassB respectively and implement them in the CPP file
B) use class ClassB at the beginning of the two header files; And the class ClassA; Statement of the other party
C) include the header file of another class in the two CPP files respectively
NOTE: in this method, it is important to remember that you cannot use the class name to define variables and function arguments. You can only use the class name to define references or Pointers.        

2) use global variables
Since global variables can avoid overreferencing, don't belabor. My habit is to add the extern statement of the class object to the end of the header file. It's not a big deal how you like to write it.

3) use base class Pointers.
This approach is to use base class Pointers everywhere that references classes in advance. In general, two classes that refer to each other do not involve their base classes, and therefore do not lead to overreferencing. Starting with the example: replace CMyView* with CView* in the CMyDialog class, and replace CMyDialog* with CMyDialog* in the CMyView class, so that there is no lead reference.

Description: In this paper, for the sake of narration, the class AClass; The statement becomes the declaration of class AClass, and the description of the class member variable and member function prototype of AClass at the beginning of class AClass is called the definition of the class, while the part in CPP is called the definition of the class. If you have different understandings of these three words, please change these three words into corresponding words according to your own original meaning.

Ps: I solved the problem in the first way.


Related articles: