Mutual call implementation between the three classes

Source: Internet
Author: User

Mutual calls between the three classes

Origin: during project development, three core classes (ClassA, ClassB, and ClassC) are defined. The Calling relationship between the three classes is as follows: ClassC is a management class and ClassA needs to be called, the ClassB interface. At the same time, as the Managed class, ClassA and ClassB, you need to call the ClassC interface.

Clarify the relationships between the three types:

1) Definition order: ClassA, ClassB prior to ClassC.

2) Call relationship: ClassC calls the ClassA interface and ClassB interface. ClassA calls the ClassC interface and classB calls the ClassC interface.

In fact, we can clarify 1) and 2) that there is no direct call relationship between ClassA and ClassB (if it can be more complex, we can deal with it ).

Core point: when two classes need to call the interface of the Peer class to each other, they need to mainly include the header file of the other party and the forward declaration. Many netizens have answered this question on the Internet. For example, the relationship between ClassA and ClassC is as follows:

Step 1: Define ClassA. h, classA. cpp, ClassC. h ClassC. cpp;

Step 2: Add # include "ClassA. h" to the ClassC. h header file and add # include "ClassC. h" to the cpp file of ClassC ";

Step 3: In ClassA. add # include "ClassA. h ", add # include" ClassA. h ", # include" ClassC. h ".

For more information, see the practice code:

Code implementation:

 

/*** ClassA. h */# ifndefCLASSA_H_H # defineCLASSA_H_H classClassC; classClassA {public: ClassA (ClassC * pC); ClassA ();~ ClassA (); public: void displayA (); void invoke_classC_in_ClassA (); public: ClassC * m_pC;}; # endif/*** ClassA. cpp */# include "stdafx. h "# include" ClassA. h "# include" classC. h "# include <iostream> usingnamespace std; ClassA: ClassA (ClassC * pC) {cout <" ClassA (ClassC * pC) constructor is running! "<Endl; m_pC = pC;} ClassA: ClassA () {cout <" ClassA () constructoris running! "<Endl;} ClassA ::~ ClassA () {cout <"ClassA destructor isrunning! "<Endl;} voidClassA: invoke_classC_in_ClassA () {cout <" ClassA: running () is running "<endl; m_pC-> displayC ();} voidClassA:: displayA () {cout <"classA: displayA () isrunning! "<Endl;}/*** ClassB. h */# ifndefCLASSB_H_H # defineCLASSB_H_H classClassC; classClassB {public: ClassB (ClassC * pC); ClassB ();~ ClassB (); public: void displayB (); void invoke_classC_in_ClassB (); public: ClassC * m_pC;}; # endif/*** ClassB. cpp */# include "stdafx. h "# include" ClassB. h "# include" classC. h "# include <iostream> usingnamespace std; ClassB: ClassB (ClassC * pC) {cout <" ClassB (ClassC * pC) constructor is running! "<Endl; m_pC = pC;} ClassB: ClassB () {cout <" ClassB () constructoris running! "<Endl;} ClassB ::~ ClassB () {cout <"ClassB destructor isrunning! "<Endl;} voidClassB: displayB () {cout <" ClassB: displayB () isrunning! "<Endl;} voidClassB: invoke_classC_in_ClassB () {cout <" ClassB: invoke_classC_in_ClassB () is running! "<Endl; m_pC-> displayC ();}/*** ClassC. h */# ifndefCLASSC_H_H # defineCLASSC_H_H # include "ClassA. h "# include" ClassB. h "classClassC {public: ClassC ();~ ClassC (); public: void displayC (); void invoke_ClassA_in_ClassC (); void invoke_ClassB_in_ClassC (); public: ClassA * m_pA; ClassB * m_pB ;}; # endif/*** ClassC. cpp */# include "stdafx. h "# include" ClassC. h "# include <iostream> usingnamespace std; ClassC: ClassC () {cout <" ClassC () constructoris running! "<Endl; m_pA = new ClassA (); m_pB = new ClassB ();} ClassC ::~ ClassC () {cout <"ClassC destructor isrunning! "<Endl; if (NULL! = M_pA) {delete m_pA; m_pA = NULL;} if (NULL! = M_pB) {delete m_pB; m_pB = NULL ;}} voidClassC: displayC () {cout <"ClassC: displayC () isrunning! "<Endl;} voidClassC: invoke_ClassA_in_ClassC () {cout <" ClassC: invoke_ClassA_in_ClassC () is running! "<Endl; m_pA-> displayA ();} voidClassC: invoke_ClassB_in_ClassC () {cout <" ClassC: invoke_ClassB_in_ClassC () is running! "<Endl; m_pB-> displayB ();}/*** main. cpp */# include "stdafx. h "# include" ClassA. h "# include" ClassB. h "# include" ClassC. h "# include <iostream> using namespacestd; intmain (int argc, char * argv []) {ClassC * pC = new ClassC; cout <" Class C calls Class: "<endl; pC-> invoke_ClassA_in_ClassC (); cout <endl <" Class C calls Class B: "<endl; pC-> invoke_ClassB_in_ClassC (); cout <endl <"Class A calls Class C:" <endl; ClassA * pA = new ClassA (pC); pA-> invoke_classC_in_ClassA (); cout <endl <"Class B calls Class C:" <endl; ClassB * pB = new ClassB (pC); pB-> invoke_classC_in_ClassB (); cout <endl; delete pB; delete pA; delete pC; return 0 ;}

 

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.