Advance reference

xiaoxiao2021-03-06  41

Source: Forget the source, it seems to be searched on the post on the 9CBS BBS

The so-called advance reference means that a type is used to define variables and declaration functions before definition.

Under normal circumstances, C / C requires all types must be defined before use, but in some special cases, this requirement cannot meet.

For example, a non-mode dialog object pointer is preserved in class CMYVIEW, which is used to display / modify some information. In order to implement dialog

The "Application" button updates the modifications made by the dialog to the View interface, for this, need to save the VIEW pointer in the dialog class, so

Defining the relationship into the following code:

#ifndef __myview_h__

#define __myview_h__

// This is the head function of the View class

#include "mydialog.h"

Class CMYVIEW :: Public CView

{

protected:

CMYDIALOG * PDLG;

/ / Here is 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 perspective of the compiler, when compiling myDialog.cpp, the system first defines the macro __mydialog_h__, then contains myView.h, myview.h

#Include "mydialog.h" is no longer played because __mydialog_h__ has been defined. In the statement of the CMYVIEW class,

CMYDIALOG * PDLG;

The compiler will generate the "cmyDialog" type error that is not defined, and the error that appears in the compilement mYView.cpp file can be similar.

More general, class A and class b need to be referenced to each other, so there is inevitable a class will be defined first, while another class is defined, so

When the class defined first is first defined, the so-called advance reference is caused.

The error caused by advance references has several processing methods:

1) Use class declarations

Before the advance references to a class, you first use a special statement to indicate that the identifier is a class name, which is about to be referenced. The method of use is:

a) Use class classb; declare the class name that is about to be advanced

b) Define Class ClassA

c) Define Class Classb;

d) Prepare two types of implementation code.

The above method applies to all code in the same file, in general, ClassA and Classb have their own header files and CPP files, this

Method needs to evolve:

a) Define ClassA and Classb, and implement it in the CPP file.

b) Use class classb; and clas classesa; declaration each other in the beginning of the two header files;

c) The header file containing another class in two CPP files, respectively.

Note: This method remembers that the class name is not used to define variable parameters of variables and functions, only to define references or pointers.

2) Use global variables

Since global variables can avoid advance reference, do not have to be described. My habit is to add the EXTERN statement of the class object in the final file, everyone likes

How to write that there is no big problem, the key is to ensure that you don't include it in the header file.

3) Use the base class pointer.

This method is to use the base class pointer in the place where the advance reference class is referenced. In general, two classes referenced by two do not involve their base classes, so they do not cause advance references. In the case of the beginning of the example: In the CMYDIALOG class instead of CMYVIEW *, in the CMYVIEW class instead of CMYDIALOG *, this is inevitable

Will not cause advance references.

Description: In this article, in order to describe convenience, Class Aclasses; statement becomes a category of ACLASS, starting Class ACLASS to ACLASS class member variables,

An illustration of a member function prototype and the like is called the definition of the class, and the part of the CPP is called the definition of the class. If you have a different understanding of these three words, please follow your original meaning

Replace these three words into corresponding words.

Latest Reviews [Published Reviews] See all comments Recommend to your friends print

agree! The object of your own class is not a member of this class (whether it is direct or indirect), but the pointer and reference can! (Tomyne Posted at 2002-12-11 14:33:00)

Thank sunshineor@msn.com (Lin Mountain Posted on 2002-11-22 9:41:00)

I said that initialization is the definition of classes in memory (sunshineor@msn.com Posted at 2002-11-21 14:09:00)

/ *

Lin Mountain:

Don't you think your definition is a dead loop?

When compiling initialization, the class object (member function) inside the class is initialized (note, it is object, not the object pointer),

ACLASS variable BOBJECT is in your program

First call the initialization function of BOBJECT, then bically calls the initialization function of the A, and so on. In this way, the compiler cannot obtain the size of the Class B

Natural compilation will fail, for an unknown size class, naturally

undefined class

With pointers,

* / (Sunshineormer Posted at 2002-11-21 14:07:00)

#include

#include

Using namespace std;

Class CA;

Class CB

{

PUBLIC:

CB () {};

~ Cb () {};

Void Display ()

{

String s = "this is class b";

Cout << s << endl;

}

Ca * aobject;

}

Class CA

{

PUBLIC:

Ca () {};

~ CA () {};

CB Bobject;

Void Display ()

{

COUT << "this is class a" << endl;

}

}

int main ()

{

CA AOBJECT;

Return 0;

} (Sunshineormer Posted at 2002-11-21 14:07:00)

I tried it, still a bit problem. Please advise. Source code is as follows

#include "stdafx.h"

Class bclass;

Class ACLASS;

Class ACLASS

{

PUBLIC:

Aclass () {};

~ Aclass () {};

BCLASS BOBJECT;

Void Display () {Printf ("this is class a / n");}

}

Class bclass

{

PUBLIC:

BCLASS () {};

~ Bclass () {}; void display () {Printf ("/ nthis is class b / n");

Aclass aobject;

}

Int main (int Argc, char * argv [])

{

Printf ("Hello World! / N");

BCLASS BOBJECT;

Bobject.aObject.display ();

Return 0;

}

Compile prompt "Error C2079: 'Bobject' Uses undefined class 'bclass'"

(Lin Mountain Published on 2002-11-21 10:23:00)

What is the relationship between the macro definition and #include "xxx.h" before the header file? The #include "mydialog.h" in MyView.h is already defined because _mydialog_h__ has been defined, so I will not play this sentence how to understand. Please advise, thank you! (Lin Yam published on 2002-11-21 9:14:00)

转载请注明原文地址:https://www.9cbs.com/read-70552.html

New Post(0)